How to Check If a File or Folder Is Empty in PowerShell

Check if a file or folder is empty in PowerShell

To check if a file is empty in PowerShell, get the file with Get-Item and test whether its Length is 0. Length is the file’s size in bytes: Output: Folders need a different test, which I cover further down. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1 against the same sample folder, … Read more

How to Get a Substring in PowerShell (Start, End, Before and After)

Substring in PowerShell examples

To get a substring in PowerShell, call the Substring method on a string. Substring(start, length) returns length characters from the start position, and Substring(start) returns everything from there to the end: Output: Positions start at 0, so position 3 is the fourth character. I ran every example here in PowerShell 7.6 and Windows PowerShell 5.1, … Read more

How to Convert a String to an Integer in PowerShell (and Decimal, Double)

Convert string to int in PowerShell

To convert a string to an integer in PowerShell, cast it with [int]. After that, math works as numbers instead of text: Output: Use [decimal] for money and [double] for other fractions. Below I cover safe conversion, rounding, commas, characters and scientific notation. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with … Read more

PowerShell Script to Delete Files Older Than 30 Days

PowerShell delete files older than 30 days

To delete files older than 30 days with PowerShell, list the files with Get-ChildItem, keep the ones whose LastWriteTime is before a cutoff date, and pipe them to Remove-Item. Run it with -WhatIf first: Output: In a real script, set the cutoff with (Get-Date).AddDays(-30). I used a fixed date and a sample folder so the … Read more

How to Create an Empty Array in PowerShell

Create an empty array in PowerShell

To create an empty array in PowerShell, assign @() to a variable. You can then add items with +=, and Count tells you how many it holds: Output: That’s fine for small lists. Below I cover typed and fixed-size arrays, a faster List, and the checks that trip people up. I ran every example in … Read more

How to Compare Two Arrays in PowerShell (Matches and Differences)

Compare two arrays in PowerShell

To compare two arrays in PowerShell, use Compare-Object. It lists the items that are only in one array: <= means only in the first (reference) array, and => only in the second: Output: WEB02 and web02 matched, because Compare-Object ignores case and order. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with … Read more

How to Remove Duplicates from an Array in PowerShell

Remove duplicates from an array in PowerShell

To remove duplicates from an array in PowerShell, pipe it to Select-Object -Unique. It keeps the first copy of each value, in the original order: Output: Notice that Austin and austin both stayed. Select-Object -Unique is case-sensitive, and each method treats case and order differently. I ran every example in PowerShell 7.6 and Windows PowerShell … Read more

How to Get the File Name from a Path in PowerShell

Get file name from path in PowerShell

To get the file name from a path in PowerShell, use Split-Path -Leaf. -Parent gives you the folder part instead: Output: Split-Path only works on the text, so the file doesn’t need to exist. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1 and note the 7-only options. Get the name without the … Read more