PowerShell Scripting Cheat Sheet
Core PowerShell scripting reference covering variables, the object pipeline, control flow, functions, and common cmdlets.
Basics
Variables and output.
# Variables and output$name = "World"Write-Host "Hello, $name!"$number = 42Write-Output "Value: $number"Set-StrictMode -Version Latest
The Pipeline
Piping objects between cmdlets.
Get-Process | Where-Object { $_.CPU -gt 100 }Get-ChildItem -Path C:\Logs -Filter *.logGet-Content .\file.txt -Tail 10$services = Get-Service$services | Sort-Object Status | Select-Object -First 5
Functions
Defining functions with typed parameters.
function Get-Square { param( [Parameter(Mandatory=$true)] [int]$Number ) return $Number * $Number}Get-Square -Number 51..5 | ForEach-Object { $_ * 2 } | Where-Object { $_ -gt 4 }
Control Flow
Branching, looping, and error handling.
- if / elseif / else- if ($x -gt 5) { } elseif ($x -eq 5) { } else { }
- foreach- foreach ($item in $collection) { } iterates a collection
- for- for ($i=0; $i -lt 5; $i++) { } classic counting loop
- switch- switch ($x) { 1 {"one"} default {"other"} } multi-branch match
- try/catch/finally- Structured error handling around a script block
Common Cmdlets
Frequently used cmdlets.
- Get-Help <cmdlet> -Full- View full documentation for a cmdlet
- Get-Command -Verb Get- List cmdlets that use the Get verb
- Invoke-WebRequest -Uri <url>- Make an HTTP request
- Import-Module <name>- Load a module into the session
- New-Item -ItemType Directory -Path .- Create a new file or folder
- $_- Refers to the current object in the pipeline
Objects, Select & Where
Filter and project the object pipeline.
Get-Process | Where-Object { $_.CPU -gt 10 } | Sort-Object CPU -Descending | Select-Object -First 5 Name, CPU, Id# calculated propertyGet-ChildItem | Select-Object Name, @{ Name='SizeKB'; Expression={ [math]::Round($_.Length/1KB, 1) } }
Comparison & Logical Operators
PowerShell uses named operators, not symbols.
- -eq / -ne- equal / not equal (case-insensitive for strings)
- -gt / -ge / -lt / -le- greater/less than, with -or-equal variants
- -like- wildcard match using * and ? patterns
- -match- regular expression match; fills $Matches on success
- -contains / -in- test collection membership of a value
- -and / -or / -not- logical combinators for boolean expressions
Error Handling
Try/catch and terminating vs non-terminating errors.
try { $content = Get-Content -Path 'missing.txt' -ErrorAction Stop}catch [System.IO.FileNotFoundException] { Write-Warning "File not found: $($_.Exception.Message)"}catch { Write-Error "Unexpected: $_"}finally { Write-Host 'done'}$ErrorActionPreference = 'Stop' # make all errors terminating
Hashtables & Splatting
Key-value maps and passing parameter bundles.
$config = @{ Name = 'server01' Port = 8080 Tags = @('web', 'prod')}$config['Name']$config.Port = 9090# splatting: pass a hashtable as named parameters$params = @{ Path = 'C:\logs'; Recurse = $true; Filter = '*.log' }Get-ChildItem @params
Advanced Functions & Parameters
Typed parameters, validation, and pipeline input.
function Get-Square { [CmdletBinding()] param( [Parameter(Mandatory, ValueFromPipeline)] [ValidateRange(0, 100)] [int]$Number ) process { [PSCustomObject]@{ Input = $Number; Square = $Number * $Number } }}1..5 | Get-Square
Use approved verb-noun naming (Get-, Set-, New-, Remove-) for custom functions — run Get-Verb to see the approved list — so your cmdlets integrate cleanly with PowerShell's discovery and help system.