PowerShell Test-Path: Check If a File or Folder Exists

PowerShell Test-Path examples

Test-Path is the PowerShell cmdlet that checks whether a file, folder or other path exists. It returns True or False, so it drops straight into an if statement: Output: It never throws an error for a missing path, which makes it the safest check before you read, copy or delete something. I ran each example … Read more

How to Set the Execution Policy in PowerShell

Set the execution policy in PowerShell

To set the execution policy in PowerShell for your own account, run Set-ExecutionPolicy with RemoteSigned and the CurrentUser scope. It doesn’t need administrator rights: RemoteSigned runs scripts you write and blocks unsigned scripts from the internet. Below I check the current policy, change it for one session only, deal with blocked downloads and explain each … Read more

PowerShell Format-Table: Columns, Sorting and Long Text

PowerShell Format-Table examples

Format-Table is the PowerShell cmdlet that shows command output as a table. Pipe objects to it and list the properties you want as columns. All the examples below use this small list of servers: With no properties listed, Format-Table shows every property that fits: Output: I ran the examples in real PowerShell 7.6 and Windows … Read more

PowerShell -match Operator: Regex Examples

PowerShell -match operator with examples

The PowerShell -match operator checks a string against a regular expression and returns True or False. When it matches, the matched text is saved in the automatic $Matches variable: Output: \d{4} means four digits, so the pattern finds the order number anywhere in the text. Below I cover case sensitivity, named groups, arrays, -notmatch, special … 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

How to Restart a Computer Using PowerShell

Restart a computer using PowerShell

To restart a computer using PowerShell, run Restart-Computer. It restarts the local PC right away, so save your work first: Add -ComputerName to restart remote PCs. Below I preview restarts safely, restart remote servers and wait for them to come back, check for a pending restart and schedule one for later. Preview a restart with … Read more

PowerShell IsNullOrEmpty vs IsNullOrWhiteSpace

PowerShell IsNullOrEmpty vs IsNullOrWhiteSpace

In PowerShell, IsNullOrEmpty vs IsNullOrWhiteSpace comes down to how they treat spaces. IsNullOrEmpty only catches $null and empty strings, while IsNullOrWhiteSpace also catches strings made only of spaces, tabs or line breaks: For user input and data from files, IsNullOrWhiteSpace is almost always the one you want. Below are both checks side by side, a … Read more

How to Split a String in PowerShell (With Examples)

Split a string in PowerShell

To split a string in PowerShell, use the -split operator with the delimiter. It returns an array, with one item per piece: Output: PowerShell also has the .Split() string method. The two behave differently in a few important ways, and one of them changed between PowerShell versions. I ran every example in PowerShell 7.6 and … Read more