Error Handling
PowerShell · Reference cheat sheet
Error Handling
PowerShell · Reference cheat sheet
📋 Overview
PowerShell has terminating and non-terminating errors. Use -ErrorAction, try/catch/finally, and $Error to control behavior. Prefer terminating errors for scripts that must stop; use -ErrorVariable to capture without stopping.
🔧 Core concepts
| Mechanism | Role |
|---|---|
| Non-terminating | Writes to error stream; continues |
| Terminating | Stops statement / enters catch |
-ErrorAction | Stop, Continue, SilentlyContinue, Inquire, Ignore |
$ErrorActionPreference | Default for non-terminating |
try / catch / finally | Structured handling |
throw / Write-Error | Raise errors |
$Error | Automatic error list |
$LASTEXITCODE | Native command exit code |
catch [System.IO.IOException] filters by type. $PSItem / $_ in catch is the error record.
💡 Examples
Try/catch:
try {
Get-Content -LiteralPath .\missing.txt -ErrorAction Stop
} catch {
Write-Error "Failed: $($_.Exception.Message)"
} finally {
'cleanup'
}Capture without stop:
Get-ChildItem .\nope -ErrorAction SilentlyContinue -ErrorVariable e
if ($e) { $e | ForEach-Object Message }Native commands:
git rev-parse HEAD
if ($LASTEXITCODE -ne 0) { throw "git failed: $LASTEXITCODE" }Preference scope:
$ErrorActionPreference = 'Stop'⚠️ Pitfalls
- Default
-ErrorAction Continuemeans scripts keep going after many failures. try/catchonly catches terminating errors—add-ErrorAction Stopto cmdlets.- Swallowing errors with
SilentlyContinuehides root causes. $Error[0]is the newest; clear carefully ($Error.Clear()).- Mixing
throwstrings vsErrorRecordchanges catch behavior slightly. - Pipeline errors may not abort the whole pipeline unless preference is
Stop.