Pcornel
Automation · PowerShell Automation

Error Handling

Cheat sheet · https://pcornel.dk/topic.php?topic=5239

Back to the Topic

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

BoundaryError Handling reference
Runtime identityPowerShell edition/version + command/module source
Persistent intentScript/module/config prerequisites under source control
VerificationRead-only discovery → execution evidence → independent target read-back
Output contractStructured objects/streams before formatting
Failure boundarySeparate 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

Confirmation

Confirm action

Continue?