liveBook · Manning

-ErrorAction

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

ErrorAction is a PowerShell common parameter that specifies how a command responds to non-terminating errors.

-ErrorAction is a PowerShell common parameter for choosing how a command responds to non-terminating errors. Its most important automation use is making a command failure visible to structured error handling instead of letting execution continue as though the command succeeded. section 6.1.1

Use -ErrorAction Stop inside a try block when a failed command must transfer control to catch. Stop turns non-terminating errors into terminating errors, so later statements in the try block are skipped and the caught error is available in $_. A finally block can then log or clean up regardless of success or failure. section 3.3.5 section 6.1.1

Choose the response based on what a failed step means to the remaining work:

  • Use Stop when a failure would invalidate later steps, such as a required setup operation whose failure would cause subsequent work to fail. section 6.1.1
  • Use non-terminating handling when later items can still be processed after one failure, then review and address the recorded failures afterward. section 6.1.1
  • Avoid treating SilentlyContinue as proof of an expected condition. Suppressing a Get-Service error can hide either a missing service or an unrelated problem such as insufficient permissions. section 6.1

Handle a missing service deliberately

A service-management script can call Get-Service -Name xyz -ErrorAction Stop in try. Catch then examines $_.FullyQualifiedErrorId: an expected missing-service error may be recorded and skipped, while an unexpected error can be sent to Write-Error or thrown. This preserves processing of other services without concealing permission or operational failures. section 6.1.1 section 6.1

Action scripts that run independently need their own error handling and logging. Place the operational function call in try, set a success message only after it completes, set an error message in catch, and write the result in finally. This leaves an audit trail whether a file operation succeeds or fails. section 3.3.5

try{
   Get-Service -Name xyz -ErrorAction Stop
}
catch{
   $_
}

This minimal pattern shows why try/catch alone is insufficient for a non-terminating Get-Service error and where -ErrorAction Stop belongs. section 6.1.1

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Explains Stop and SilentlyContinue through service and action-script workflows, deferring broader logging treatment.
Examples
Uses missing-service lookups and file-moving action scripts with logging to demonstrate error responses.
What it assumes
Assumes PowerShell command and function use while explaining non-terminating errors and try/catch behavior through examples.
What you can do after
Apply it via PowerShell commands to route expected and unexpected failures through appropriate handling paths.
How it connects
Connects -ErrorAction with try/catch/finally, $_, Write-Error, throw, and non-terminating errors.

Also explained in

Compare how 3 books explain -ErrorAction →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.