PowerShell Date Comparison: Compare Dates the Right Way

PowerShell date comparison

To compare dates in PowerShell, make sure both sides are [datetime] values, then use -lt, -gt and -eq. Subtracting one date from another tells you how far apart they are: Output: Earlier means smaller, so -lt reads as “before” and -gt as “after.” I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with … Read more

How to Compare Strings in PowerShell

Compare strings in PowerShell

To compare strings in PowerShell, use -eq for equal and -ne for not equal. Both ignore case, so add a c for a case-sensitive comparison, like -ceq: Output: I ran every example in PowerShell 7.6 and Windows PowerShell 5.1. Everything matched except one .NET method overload, which I point out below. Case-sensitive and case-insensitive comparison … Read more

PowerShell Add-Content: Append Text to a File

PowerShell Add-Content: append text to a file

To append text to a file in PowerShell, use Add-Content with the file path and the text. It adds the text as a new line at the end, and leaves everything already in the file alone: Output: I ran every example in PowerShell 7.6 and Windows PowerShell 5.1. Most behave the same, but mixing Add-Content … Read more

How to Remove Characters from Strings in PowerShell

Remove characters from strings in PowerShell

To remove characters from a string in PowerShell, replace them with nothing. Use .Replace() for exact characters, or -replace with a pattern to remove a whole group at once: Output: The second line keeps only the digits, which is the easiest way to clean up a US phone number. I ran every example in PowerShell … Read more

How to Get All Files in a Directory Using PowerShell

Get all files in a directory using PowerShell

To get all files in a directory with PowerShell, run Get-ChildItem with the folder path and -File. -File leaves out the subfolders: Output: Without -File, you’d get the Archive and node_modules folders in the list too. Use -Directory when you want only folders. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1. Include … Read more

How to Get the Length of a String in PowerShell

Get the length of a string in PowerShell

To get the length of a string in PowerShell, read its .Length property. It returns the number of characters, including spaces: Output: That works for everyday text. Arrays, numbers, emoji and byte counts behave differently, and I cover each one below. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with the same … Read more

How to Replace Text in Strings Using PowerShell

Replace text in strings using PowerShell

To replace text in a string in PowerShell, use the .Replace() method or the -replace operator. Both return a new string with the changes: Output: They look interchangeable, but they aren’t. .Replace() matches exact text and cares about case. -replace uses regular expressions and ignores case. I ran every example in PowerShell 7.6 and Windows … Read more

How to Extract Text Between Delimiters in PowerShell

Extract text between delimiters in PowerShell

To extract text between two delimiters in PowerShell, match it with a regular expression and a capture group. The part in parentheses lands in $Matches[1]: Output: The ? after .* matters more than it looks, and -match only returns the first match. Both are covered below. I ran every example in PowerShell 7.6 and Windows … Read more

PowerShell ForEach-Object: Examples and foreach vs ForEach-Object

PowerShell ForEach-Object with examples

ForEach-Object runs a script block once for every object that comes down the pipeline. Inside the block, $_ is the current object: Output: $PSItem is another name for $_, if you find it easier to read. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, and I note where they differ. Work with … Read more