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

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

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

PowerShell Where-Object vs Select-Object: Rows vs Properties

PowerShell Where-Object vs Select-Object

The short version of Where-Object vs Select-Object: Where-Object decides which objects (rows) stay in the pipeline, and Select-Object decides which properties (columns) each object keeps. Most real commands use both, filter first: I’ll show what each cmdlet returns, why the order matters, the row tricks Select-Object also has, and one version difference between PowerShell 7 … Read more

How to Expand an Array in PowerShell (-ExpandProperty and More)

How to expand an array in PowerShell

To expand an array in PowerShell, pipe the objects to Select-Object -ExpandProperty and name the property that holds the array. You get every value as its own item instead of a {a, b} blob: Below I cover dot notation, keeping the parent value, the {a, b, c…} display limit, flattening nested arrays and growing an … Read more