Echo in PowerShell: How Write-Output Works (With Examples)

Echo in PowerShell and Write-Output cmdlet examples

You type echo in PowerShell out of CMD or Bash habit, and it mostly works. Then a sentence splits across three lines, or echo -n does nothing you expected. Quick answer: echo is a built-in alias for Write-Output. It sends objects to the success stream (the pipeline). If nothing consumes them, they’re shown in the … Read more

PowerShell Escape Character (Backtick): Escape Special Characters

PowerShell escape character (backtick) examples

The PowerShell escape character is the backtick (`), the key just below Esc on a US keyboard. Inside double quotes, a backtick makes the next character literal, like `$, or turns it into a special character, like `t for a tab: Output: A backslash is just a normal character in PowerShell, so Windows paths need … Read more

Create a Directory in PowerShell with New-Item (and mkdir)

Create a directory in PowerShell with New-Item

Creating a directory in PowerShell takes one command: New-Item -Path C:\Reports -ItemType Directory. Change the item type to File and the same cmdlet creates a file. Here are both in one script: Output: New-Item hands back the new item, a DirectoryInfo for a folder and a 0-byte FileInfo for a file, so you can use … Read more

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 Join and Concatenate Strings in PowerShell (-join, +, -f)

Join and concatenate strings in PowerShell with the -join operator

To join strings in PowerShell, put the -join operator between a list and the separator you want. For two or three values, double quotes or the + operator concatenate them just as well, and -f builds a string from a template: Output: Those four cover nearly every case. The details below matter when the list … 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