PowerShell Do-While, While and Do-Until Loops (With Examples)

PowerShell do-while, while and do-until loops

A do-while loop in PowerShell runs its block once, then keeps repeating it as long as the condition after while is true. Because the check comes at the end, the block always runs at least one time: Output: A plain while loop checks its condition first instead, so its block may never run. Below I’ll … Read more

PowerShell Hashtable (Dictionary): Create, Add, Loop and Sort

PowerShell hashtable created, filled in a loop and sorted

Think of a hashtable in PowerShell as a lookup list: each value sits under a key, and you get it back by that key instead of by position. Create one with @{ Key = ‘Value’ } and read it with $table[‘Key’]: Output: A hashtable is PowerShell’s built-in dictionary, so you’ll also see it called a … Read more

PowerShell -in, -notin and -contains Operators (With Examples)

PowerShell -in and -contains operator examples

The PowerShell -in operator checks whether a value is in a collection, and -contains runs the same check with the collection written first. Both return True or False: They compare whole items, never parts of text, and that trips up a lot of scripts. I ran each example below in PowerShell 7 and Windows PowerShell … Read more

How to Convert Variables to Strings in PowerShell

Convert a variable to a string in PowerShell

To convert a variable to a string in PowerShell, cast it with [string], call its ToString() method, or put it inside double quotes. For a number, all three give the same text: Output: They don’t always agree, though. Dates, decimals, arrays and $null each behave a little differently depending on which one you use, and … Read more

Get-FileHash in PowerShell: Get the SHA256 or MD5 Hash of a File

Get-FileHash in PowerShell: get the SHA256 or MD5 hash of a file

Get-FileHash is the built-in PowerShell cmdlet that calculates a file’s hash. Give it a path and you get a SHA256 hash; add -Algorithm MD5 when a download page lists an MD5 checksum: A hash is a fingerprint of a file’s content. Rename the file and the hash stays the same, but change a single character … Read more

Delete a Directory in PowerShell: Remove-Item for Folders and Files

Delete a directory in PowerShell with Remove-Item -Recurse

Remove-Item with -Recurse is how you delete a directory in PowerShell, files and subfolders included. Add -Force so hidden and read-only files don’t stop it: Output: There’s no Recycle Bin and no undo. I ran everything below in a throwaway test folder, C:\psfaqs\Purge, in PowerShell 7.6 and Windows PowerShell 5.1. Where the two behaved differently, … Read more

PowerShell ForEach Loop: Examples, 1..10 Ranges and Traps

PowerShell foreach loop with the 1..10 range operator

A PowerShell foreach loop repeats a block of code once for each item in a collection: an array, a folder of files, command output, or a number range such as 1..10. The current item lands in the variable you name before in: Here’s a real one that walks through an array of office names: Output: … Read more

Grep in PowerShell: Select-String Examples for Searching Text

Grep in PowerShell with Select-String examples

PowerShell doesn’t ship a grep command, but Select-String does the same job. Give it a pattern and a file, and it returns every matching line, the way grep in PowerShell should: Below, I map the grep options you already know to Select-String parameters and run each one on a sample log. Every result came from … Read more