Automation · PowerShell Automation
Error Handling
Cheat sheet · https://pcornel.dk/topic.php?topic=5239
Answer
PowerShell error handling distinguishes terminating and non-terminating errors. Use -ErrorAction Stop or $ErrorActionPreference = 'Stop' so try/catch can catch failures.
Remember
- Non-terminating errors are not caught by try/catch by default.
- Use -ErrorAction Stop or $ErrorActionPreference = 'Stop'.
- $Error[0] holds the latest error record.
First checks
$ErrorActionPreference— stop on errors?$Error[0] | Format-List * -Force— last error details- -ErrorAction Stop on critical calls — terminating errors
Verify in this order
error → terminating (Stop) → catch → handle
Where it usually breaks
Script/input → command/module binding → local/remoting session → request accepted → command result → target object read-back → effective runtime state → workload.
Error Handling · PowerShell operational reference
| Boundary | Error Handling reference |
|---|---|
| Runtime identity | PowerShell edition/version + command/module source |
| Persistent intent | Script/module/config prerequisites under source control |
| Verification | Read-only discovery → execution evidence → independent target read-back |
| Output contract | Structured objects/streams before formatting |
| Failure boundary | Separate engine/binding from transport/API/native and target-state failures |
Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5239