How to Pick Random Items from an Array in PowerShell

Pick random items from an array in PowerShell with Get-Random

To pick random items from an array in PowerShell, pipe the array to Get-Random. Add -Count to get several different items at once: That covers most scripts. Below I’ll show repeats, shuffling, repeatable picks, weighted choices, and the places where Windows PowerShell 5.1 behaves differently from PowerShell 7. How do I pick a random item … Read more

How to Get Unique Values from a CSV in PowerShell

Get unique values from a CSV file in PowerShell

To get unique values from a CSV in PowerShell, import the file and expand one column with the -Unique switch: That’s the whole trick for a single column. The rest of this guide covers sorted lists, duplicate rows, several columns, counts, and the case-sensitivity traps I hit in PowerShell 7 and Windows PowerShell 5.1. How … Read more

PowerShell Format-List: Show Object Properties as a List

PowerShell Format-List cmdlet shows object properties as a list

PowerShell Format-List shows each property of an object on its own line, which makes wide objects easy to read. Pipe any output to it, or use its alias fl: I tested every example below in PowerShell 7.6 and Windows PowerShell 5.1 with demo employee objects. What does Format-List do in PowerShell? Format-List prints label : … Read more

How to Get JSON Data from a URL in PowerShell

Get JSON data from a URL in PowerShell with Invoke-RestMethod

To get JSON data from a URL in PowerShell, call Invoke-RestMethod. It downloads the response and converts the JSON into objects in one step, so there’s no ConvertFrom-Json to run: I ran every example below in PowerShell 7.6 and Windows PowerShell 5.1 against free public test APIs, so you can paste them as they are. … Read more

How to Create a Folder If It Doesn’t Exist in PowerShell

Create a folder only if it doesn't exist in PowerShell

To create a folder if it does not exist in PowerShell, check it with Test-Path -PathType Container and run New-Item when the check comes back False: That pattern is safe to run on every script start. Below I cover the -Force shortcut, the .NET method, a reusable function, and two traps I hit while testing … Read more

PowerShell Cannot Index into a Null Array: Causes and Fixes

Fix the PowerShell Cannot index into a null array error

“Cannot index into a null array” means you used square brackets on a variable that holds $null, so there’s no array to read from. Check the value first, or force it into an array with @(): The real work is finding out why the variable is empty. Below are the usual causes, the fixes I … Read more

PowerShell Convert String to URI: Cast, Encode and Validate

PowerShell convert string to URI examples

To convert a string to a URI in PowerShell, cast it with the [uri] type accelerator. You get a System.Uri object with the scheme, host, path and query already split out: That works the same in PowerShell 7 and Windows PowerShell 5.1. The catch is in the details: the cast doesn’t really validate, and URL … Read more

PowerShell Get-Date UTC: Get, Format and Convert UTC Time

PowerShell Get-Date UTC examples

Need the current time in UTC? In PowerShell 7.1 and later, Get-Date -AsUTC returns it directly. Windows PowerShell 5.1 doesn’t have that switch, so use [datetime]::UtcNow there, which works in every version: Both return a DateTime whose Kind is Utc. Below I’ll format UTC values, convert them to and from US time zones, parse UTC … Read more

How to Create Multiple Folders in PowerShell (New-Item, Loops, CSV)

Create multiple folders in PowerShell

To create multiple folders in PowerShell, give New-Item a comma-separated list of paths: New-Item -ItemType Directory -Path C:\Data\A, C:\Data\B. The -Path parameter takes an array, so one command makes them all. For dozens of folders, read the names from a list, a number range or a CSV and loop over them. I tested each method … Read more

PowerShell Get-Process: List, Filter, Sort and Stop Processes

PowerShell Get-Process cmdlet

PowerShell Get-Process lists the processes running on your computer. Type Get-Process for all of them, or Get-Process -Name chrome for one app by name. Each result is a Process object you can sort, filter or stop. I’ll go through names, IDs, sorting by CPU and memory, owners, modules and remote computers. I ran each example … Read more