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

How to Run a PowerShell Script from CMD (With Parameters)

Run a PowerShell script from cmd with parameters

To run a PowerShell script from cmd, type powershell with -File and the script path, then any parameters: Swap powershell for pwsh to use PowerShell 7. I ran every command below in a real Command Prompt window, and the two versions don’t always agree on paths, switches and exit codes. Run a PowerShell script from … Read more

PowerShell Function Parameters: Create Functions with param Blocks

PowerShell function parameters in a param block

PowerShell function parameters live in a param() block at the top of the function. Each one is a variable with an optional type and default value, and you pass it by name when you call the function: Output: That’s the whole idea. Once a function has parameters, it acts like a small cmdlet, with named … 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

How to Convert pwdLastSet to a Date in PowerShell

Convert pwdLastSet to a date in PowerShell

To convert pwdLastSet to a date in PowerShell, pass the number to [datetime]::FromFileTimeUtc(). pwdLastSet is a Windows FILETIME, the count of 100-nanosecond ticks since January 1, 1601 in UTC: Below I convert it to Eastern time, handle the special 0 value, turn a date back into a FILETIME for LDAP filters and build a password … Read more

How to Read a JSON File into an Array in PowerShell

Reading a JSON file into a PowerShell array with ConvertFrom-Json

To read a JSON file into an array in PowerShell, read the whole file as one string with Get-Content -Raw and pipe it to ConvertFrom-Json. A JSON array becomes a PowerShell array of objects: That one line covers most files. The rest of this guide handles the cases that break it: one-item arrays, nested arrays, … Read more