Error handling is an important when writing scripts. Many automation scripts interact with external systems such as Active Directory, Azure services, databases, or REST APIs. When something goes wrong, the script should detect the error and react accordingly.
PowerShell provides structured error handling through Try, Catch, and Finally blocks. However, there is an important detail that is often overlooked: not all PowerShell errors are terminating errors.
Non-Terminating Errors
By default, many PowerShell cmdlets produce non-terminating errors. This means that even when a command fails, the script continues to run.
try { Get-ADUser -Identity "UserThatDoesNotExist"}catch { Write-Output "Error detected"}
In this example, the Catch block may not execute because the error produced by the cmdlet is non-terminating. The command fails, but the script continues running.
The Role of $ErrorActionPreference
The variable $ErrorActionPreference controls how PowerShell responds to non-terminating errors. It determines whether errors should be ignored, displayed, or treated as terminating errors.
$ErrorActionPreference = 'Stop'
When this setting is applied, PowerShell treats non-terminating errors as terminating errors. This ensures that the script stops immediately and that the Catch block can handle the error.
Available ErrorActionPreference Values
The $ErrorActionPreference variable supports several values that control how PowerShell reacts to errors.
Continue (Default)
This is the default behavior. PowerShell displays the error but continues executing the script.
$ErrorActionPreference = 'Continue'
This behavior is useful during interactive sessions but can be problematic in automation scripts because failures might go unnoticed.
Stop
The Stop value converts non-terminating errors into terminating errors. This immediately stops the script and allows the error to be handled in a Catch block.
$ErrorActionPreference = 'Stop'
This setting is commonly used in automation scripts where failures should stop execution to prevent inconsistent results.
SilentlyContinue
The SilentlyContinue option suppresses error messages and allows the script to continue running.
$ErrorActionPreference = 'SilentlyContinue'
This option can be useful when errors are expected and should not interrupt the script. However, it should be used carefully because it can hide important issues.
Ignore
The Ignore value suppresses the error completely and does not store it in the error variable.
$ErrorActionPreference = 'Ignore'
This option is rarely used in scripts because it completely hides errors.
Inquire
The Inquire option prompts the user whenever an error occurs and asks how to proceed.
$ErrorActionPreference = 'Inquire'
This setting is mainly useful in interactive sessions where manual decision making is required.
Break
The Break option enters the debugger when an error occurs.
$ErrorActionPreference = 'Break'
This can be helpful when troubleshooting scripts and investigating unexpected errors.
Using -ErrorAction on Individual Commands
Instead of changing the global behavior, PowerShell also allows controlling error handling for individual commands using the -ErrorAction parameter.
Get-ADUser -Identity "UserThatDoesNotExist" -ErrorAction Stop
This approach is often preferred when only a specific command should behave differently.
Example with Structured Error Handling
$ErrorActionPreference = 'Stop'try { Get-ADUser -Identity "UserThatDoesNotExist"}catch { Write-Output "An error occurred while retrieving the user."}
With this configuration, the script stops when the command fails and the error can be handled properly in the Catch block.
When to Use $ErrorActionPreference = ‘Stop’
Setting $ErrorActionPreference to Stop is especially useful in automation scenarios where scripts must react immediately to failures.
- Automation scripts
- Azure Automation runbooks
- Provisioning scripts
- Deployment pipelines
In these environments, continuing execution after a failed command may lead to incomplete configurations or inconsistent states.
Summary
PowerShell distinguishes between terminating and non-terminating errors. Because many cmdlets produce non-terminating errors by default, error handling with Try and Catch may not work as expected.
The $ErrorActionPreference variable allows controlling how PowerShell reacts to these errors. Setting it to Stop ensures that failures immediately interrupt script execution and can be handled properly.
Understanding this behavior helps build more reliable and predictable scripts