- Courses
- đź’»Programming Languages
- 🪟PowerShell
- Scripting Fundamentals
- Error Handling & Debugging
- ErrorActionPreference
On this page
PowerShell $ErrorActionPreference – Complete Guide
Published on August 1, 2026
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
PowerShell's $ErrorActionPreference is the central control mechanism for how the engine responds to errors across an entire scope. It determines whether a non‑terminating error is displayed, suppressed, escalated to a terminating error, or triggers an interactive prompt. The default value is Continue, meaning PowerShell displays the error message in red and proceeds to the next statement. This variable affects all commands in the current scope and its child scopes, and it applies to both non‑terminating and statement‑terminating errors — unlike the -ErrorAction common parameter, which only overrides behaviour for a single command and only affects non‑terminating errors. Understanding the six valid values, the scope rules, the critical asymmetry with -ErrorAction, and how native commands fit into the picture is essential for writing robust, predictable PowerShell scripts.
The Three Categories of PowerShell Errors​
PowerShell distinguishes three error categories, each with different default behaviour and handling requirements:
Non‑terminating errors report a problem but allow the pipeline and script to continue. They are generated by Write-Error, by $PSCmdlet.WriteError() in advanced functions, and by cmdlets that encounter recoverable failures on individual input objects. By default, PowerShell displays the error message and continues executing. Non‑terminating errors do not trigger catch or trap by default.
Statement‑terminating errors stop the current statement (pipeline) from running, but execution continues at the next statement in the script. They are generated by $PSCmdlet.ThrowTerminatingError() in advanced functions and compiled cmdlets, by engine errors such as CommandNotFoundException and ParameterBindingException, and by .NET method calls that throw exceptions. These errors can be caught by try/catch and trap.
Script‑terminating errors unwind the entire call stack. Execution stops completely unless the error is caught by a try/catch block or a trap statement. They are generated by the throw keyword, by parse errors, and by non‑terminating errors escalated via -ErrorAction Stop or $ErrorActionPreference = 'Stop' in non‑advanced contexts.
Understanding these categories is essential because $ErrorActionPreference interacts with each differently.
All Valid Values for $ErrorActionPreference​
$ErrorActionPreference accepts the following values from the ActionPreference enumeration:
ValueBehaviourContinueDisplay the error message and continue executing. This is the default.****StopDisplay the error message and stop executing. Escalates non‑terminating errors to script‑terminating errors. Also generates an ActionPreferenceStopException in the error stream.SilentlyContinueSuppress the error message, add the error record to $Error, and continue.IgnoreSuppress the error message and do not add the error to $Error (for non‑terminating errors). Continue executing.InquireDisplay the error message and prompt the user for a decision before continuing.BreakEnter the PowerShell debugger when an error occurs, enabling interactive investigation of the script state at the point of failure.
Note:
Suspendis not valid for$ErrorActionPreference. It is only accepted by the-ErrorActionparameter in the context of PowerShell Workflows, which are not supported in PowerShell 6 and later. Do not use it with the preference variable.
The Default: Continue​
When PowerShell starts, $ErrorActionPreference is set to Continue. Under this setting, non‑terminating errors display a red error message in the console, and execution proceeds to the next statement. The error record is added to the $Error automatic variable.
Remove-Item "C:\NoSuchFile.txt"
"Still running"
Output:
Remove-Item : Cannot find path 'C:\NoSuchFile.txt' because it does not exist.
Still running
Stop – Escalate All Errors to Terminating​
Setting $ErrorActionPreference = 'Stop' instructs PowerShell to treat every non‑terminating error as a terminating error. The script halts at the point of failure. This is the setting to use at the top of a script when you want any failure to halt execution immediately, and when you want try/catch to catch non‑terminating errors from cmdlets without adding -ErrorAction Stop to every individual command.
$ErrorActionPreference = "Stop"
Remove-Item "C:\NoSuchFile.txt"
"Still running"
Execution stops before "Still running" is reached. PowerShell additionally generates an ActionPreferenceStopException object and writes it to the error stream alongside the original error.
How Escalation Works​
When $ErrorActionPreference = 'Stop' is in effect:
- A cmdlet calls
WriteError()internally to emit what would normally be a non‑terminating error. - The PowerShell engine checks the effective
ErrorActionpreference for the current scope. - Because the preference is
Stop, the engine wraps the original error in anActionPreferenceStopException. - The
catchblock receives this exception. The original error information remains accessible through$_.Exception.ErrorRecord.
The scope of escalation depends on the calling context:
- In a non‑advanced script block or function (one without
[CmdletBinding()]),$ErrorActionPreference = 'Stop'escalates to a script‑terminating error that propagates up the entire call stack. - In an advanced function or script block (one with
[CmdletBinding()]), the escalated error remains statement‑terminating — execution continues at the next statement after the failing call.
SilentlyContinue – Suppress the Error Message​
SilentlyContinue suppresses the visible error message and allows execution to continue. The error record is still added to the $Error automatic variable, making it inspectable after the fact.
$ErrorActionPreference = "SilentlyContinue"
Remove-Item "C:\NoSuchFile.txt"
"Still running"
Output:
Still running
No error message is displayed. However, $Error[0] contains the error record, which can be inspected for logging or conditional logic.
Use SilentlyContinue when a failure is expected, harmless, and you may still want to inspect it later. Be selective about using it — this setting can make troubleshooting much harder.
Ignore – Suppress Without Recording​
Ignore goes one step further than SilentlyContinue. For non‑terminating errors, it suppresses both the display and the $Error recording. The error disappears entirely at the non‑terminating level.
$ErrorActionPreference = "Ignore"
Remove-Item "C:\NoSuchFile.txt"
"Still running"
Output:
Still running
$Error is not updated. try/catch blocks are not triggered by errors suppressed with Ignore, because the error never reaches terminating status.
Important nuance:
Ignoreonly prevents$Errorrecording for non‑terminating errors. Terminating errors that are suppressed byIgnoreare still recorded in$Error. Do not rely onIgnoreto prevent all$Errorentries in every scenario.
Use Ignore only when you are absolutely certain the error is irrelevant and you do not want any trace of it — not in $Error, not in logs, not in try/catch.
Inquire – Prompt Before Continuing​
Inquire pauses script execution when an error occurs and presents an interactive prompt:
Remove-Item : Cannot find path 'C:\NoSuchFile.txt' because it does not exist.
Confirm
[Y] Yes [A] Yes to All [H] Halt Command [S] Suspend [?] Help (default is "Y"):
The user can choose to continue, halt the command, or enter a suspended state for investigation.
Inquire is intended for interactive debugging sessions. It is not appropriate for unattended scripts or scheduled tasks — the script will hang indefinitely at the prompt.
Break – Enter the Debugger​
Break instructs PowerShell to enter the interactive debugger at the point where the error occurs. This allows inspection of the call stack, local variables, and execution state at the exact moment of failure.
$ErrorActionPreference = "Break"
Remove-Item "C:\NoSuchFile.txt"
When the error occurs, the PowerShell debugger activates. Type h at the debugger prompt for the list of available debugger commands, or q to quit.
Break is a diagnostic tool for development and troubleshooting. Do not use it in production scripts.
Overriding $ErrorActionPreference Per Command​
The -ErrorAction common parameter (alias -ea) overrides $ErrorActionPreference for a single command, regardless of the global setting:
$ErrorActionPreference = "Continue"
Remove-Item "C:\NoSuchFile.txt" -ErrorAction Stop
"Still running"
Even though the global preference is Continue, this specific Remove-Item call stops on error. The string "Still running" is never reached.
Conversely, you can suppress individual commands while running with a strict global policy:
$ErrorActionPreference = "Stop"
Remove-Item "C:\NoSuchFile.txt" -ErrorAction SilentlyContinue
"Still running" # This runs because -ErrorAction overrides Stop for this command
The $ErrorActionPreference Asymmetry with -ErrorAction​
This is the most frequently misunderstood behaviour in PowerShell error handling, and the source of many hard‑to‑diagnose bugs.
-ErrorAction only affects non‑terminating errors. When a cmdlet calls $PSCmdlet.ThrowTerminatingError() internally, -ErrorAction is ignored (except for Break). The error is always thrown regardless of what value you pass to -ErrorAction.
$ErrorActionPreference affects both non‑terminating and statement‑terminating errors. The engine's statement‑level error handler reads $ErrorActionPreference and can suppress a statement‑terminating error when the value is SilentlyContinue or Ignore.
function Test-Asymmetry {
[CmdletBinding()]
param()
# ThrowTerminatingError() ignores -ErrorAction (except Break)
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.InvalidOperationException]::new("test error"),
"TestError",
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$null
)
)
}
# -ErrorAction SilentlyContinue does NOT suppress this error:
Test-Asymmetry -ErrorAction SilentlyContinue # Error is still thrown
# $ErrorActionPreference DOES suppress this error:
$ErrorActionPreference = "SilentlyContinue"
Test-Asymmetry # Error is silently suppressed
$ErrorActionPreference = "Continue" # Restore the default
The practical consequence: if you are trying to suppress errors from a cmdlet that uses ThrowTerminatingError() internally, -ErrorAction SilentlyContinue will not work. You must set $ErrorActionPreference = 'SilentlyContinue' in the enclosing scope.
$ErrorActionPreference With try/catch​
try/catch only catches terminating errors. Non‑terminating errors pass straight through a try block without triggering any catch clause:
# Non-terminating: catch is NOT triggered
try {
Write-Error "Something went wrong"
"Execution continues here"
} catch {
"Caught: $($_.Exception.Message)" # Never reached
}
To make all cmdlet errors catchable without adding -ErrorAction Stop to every command, set $ErrorActionPreference = 'Stop' at the top of the try block or the enclosing scope:
$ErrorActionPreference = "Stop"
try {
Remove-Item "C:\NoSuchFile.txt"
}
catch {
"Caught: $($_.Exception.Message)"
}
You can also catch specific exception types for granular error handling:
try {
Get-Item -Path "C:\NoSuchFile.txt" -ErrorAction Stop
}
catch [System.Management.Automation.ItemNotFoundException] {
"File not found: $($_.Exception.Message)"
}
catch {
"Unexpected error: $($_.Exception.Message)"
}
Typed catch blocks are evaluated in order. The first matching type wins. Place more specific exception types before more general ones.
Scope Behaviour​
$ErrorActionPreference follows PowerShell's standard scoping rules:
- Setting it inside a function affects only that function's scope and any child scopes it creates.
- Setting it at the top of a script affects the entire script.
- Setting it at the interactive prompt affects the current session globally.
function Test-Scope {
$ErrorActionPreference = "SilentlyContinue"
Remove-Item "C:\NoSuchFile.txt"
"Inside function: no error shown"
}
Test-Scope
"Outside function: $ErrorActionPreference" # Still "Continue"
Remove-Item "C:\NoSuchFile.txt" # Error message appears
The change inside Test-Scope does not affect the caller's scope. The outer $ErrorActionPreference remains Continue.
To make a preference setting persistent across all PowerShell sessions, add the assignment to your PowerShell profile ($PROFILE). Understand, however, that scripts relying on the default Continue value will behave differently when run in a session where the profile has changed the preference.
Native Command Errors and $PSNativeCommandUseErrorActionPreference​
Native (external) programs — such as git, curl, or any .exe — do not participate in PowerShell's error system. They report failure through a non‑zero exit code stored in $LASTEXITCODE. By default, a non‑zero exit code does not generate an error record, does not set $Error, and does not trigger catch.
PowerShell 7.4 promoted the $PSNativeCommandUseErrorActionPreference variable to a stable feature. When set to $true, a non‑zero exit code from a native program emits a non‑terminating NativeCommandExitException error. That error then respects $ErrorActionPreference, so setting Stop makes native command failures catchable:
$PSNativeCommandUseErrorActionPreference = $true
$ErrorActionPreference = "Stop"
try {
git clone https://invalid.example.com/repo.git
}
catch {
"Git failed: $($_.Exception.Message)"
}
Without $PSNativeCommandUseErrorActionPreference = $true, the catch block is never reached regardless of the exit code. Check native command failures manually with $LASTEXITCODE when this variable is $false (the default):
git clone https://invalid.example.com/repo.git 2>$null
if ($LASTEXITCODE -ne 0) {
Write-Error "git failed with exit code $LASTEXITCODE"
}
Error‑Related Automatic Variables​
$ErrorActionPreference works alongside several automatic variables that expose error state:
VariableContents$ErrorAn ArrayList of the most recent error records. $Error[0] is the most recent. Holds up to $MaximumErrorCount entries (default 256).$?``$true if the last operation succeeded; $false if it produced any error. For native commands, reflects the exit code: $true for 0, $false otherwise.$LASTEXITCODEThe exit code of the last native program that ran. Not affected by PowerShell cmdlet errors.
Ignore prevents non‑terminating errors from being added to $Error. SilentlyContinue suppresses display but still populates $Error. Knowing which variable to inspect after suppressing an error determines which suppression strategy is appropriate.
Common Mistakes to Avoid​
Setting $ErrorActionPreference = 'Stop' globally without restoring it. If your script sets Stop at the top and calls functions or modules that expect Continue behaviour, those callers may break. Set the preference at the narrowest scope necessary and restore it afterward, or use -ErrorAction Stop on specific commands instead.
Using Ignore when you need SilentlyContinue. If you suppress an error with Ignore and later need to inspect $Error for logging or diagnostic purposes, the record will not be there. Use SilentlyContinue when you want the error suppressed but potentially inspectable.
Relying on -ErrorAction SilentlyContinue to suppress terminating errors from cmdlets. -ErrorAction does not affect ThrowTerminatingError() calls. Use $ErrorActionPreference at the scope level for that.
Using Inquire in a script meant to run unattended. The script will hang at the interactive prompt indefinitely with no way to recover.
Assuming non‑zero exit codes from native programs respect $ErrorActionPreference. They do not, unless $PSNativeCommandUseErrorActionPreference is set to $true.
Quick Reference​
ValueDisplays errorAdds to $ErrorTriggers catchContinuesContinueYesYesNo (non‑terminating)YesStopYesYesYes (after escalation)NoSilentlyContinueNoYesNoYesIgnoreNoNo (non‑terminating only)NoYesInquireYesYesNoDepends on userBreakYesYesNoDebugger opens
Frequently Asked Questions​
What is the default value of $ErrorActionPreference in PowerShell?
The default value is `Continue`. This means PowerShell displays non‑terminating error messages in red text and continues executing the script or command without interruption. The error record is added to the `$Error` automatic variable.
What are all the valid values for $ErrorActionPreference?
The six valid values are: `Continue` (default — display error and continue), `Stop` (escalate to terminating error and halt), `SilentlyContinue` (suppress display, still record in `$Error`), `Ignore` (suppress display and do not record non‑terminating errors in `$Error`), `Inquire` (prompt the user before continuing), and `Break` (enter the debugger). `Suspend` is not valid for `$ErrorActionPreference`.
What is the difference between $ErrorActionPreference and -ErrorAction?
`$ErrorActionPreference` applies to all commands in the current scope and affects both non‑terminating and statement‑terminating errors. `-ErrorAction` overrides the preference for a single command only and only affects non‑terminating errors. When a cmdlet uses `ThrowTerminatingError()` internally, `-ErrorAction` is ignored (except for `Break`), while `$ErrorActionPreference` can still suppress it.
How do I make try/catch work with cmdlet errors in PowerShell?
By default, `try/catch` only catches terminating errors. Most cmdlet errors are non‑terminating and pass straight through a `try` block. To make them catchable, either set `$ErrorActionPreference = 'Stop'` in the enclosing scope, or add `-ErrorAction Stop` to each individual command inside the `try` block. Both approaches escalate the non‑terminating error to a terminating error that `catch` can handle.
What is the difference between SilentlyContinue and Ignore?
Both suppress the error message and allow execution to continue. The difference is in `$Error` recording: `SilentlyContinue` still adds the error record to the `$Error` automatic variable, making it inspectable afterward. `Ignore` does **not** add non‑terminating errors to `$Error` at all — the error disappears entirely at the non‑terminating level. Note that terminating errors suppressed by `Ignore` are still recorded in `$Error`.
Does $ErrorActionPreference affect native command errors like git or curl?
Not by default. Native programs report failure through exit codes stored in `$LASTEXITCODE`, which does not participate in PowerShell's error system. `$ErrorActionPreference` has no effect on native command exit codes unless you also set `$PSNativeCommandUseErrorActionPreference = $true`, a stable feature introduced in PowerShell 7.4. When enabled, a non‑zero exit code emits a `NativeCommandExitException` error that respects `$ErrorActionPreference`.
What does $ErrorActionPreference = 'Stop' do exactly?
It instructs PowerShell to escalate every non‑terminating error to a terminating error, halting execution at the point of failure. It also generates an `ActionPreferenceStopException` alongside the original error. In non‑advanced script blocks, this escalation propagates up the entire call stack. In advanced functions with `[CmdletBinding()]`, the escalated error remains statement‑terminating — stopping only the current statement.
Why does -ErrorAction SilentlyContinue not suppress some errors?
Because `-ErrorAction` only affects non‑terminating errors. When a cmdlet internally calls `$PSCmdlet.ThrowTerminatingError()`, it generates a statement‑terminating error that bypasses the `-ErrorAction` parameter entirely. To suppress those errors, you must set `$ErrorActionPreference = 'SilentlyContinue'` in the enclosing scope, because `$ErrorActionPreference` applies to both non‑terminating and statement‑terminating errors.
Can I catch specific exception types when using $ErrorActionPreference = 'Stop'?
Yes. When a non‑terminating error is escalated to terminating by `Stop`, the original exception type is preserved inside the `ActionPreferenceStopException` wrapper. PowerShell unwraps the outer exception when matching typed `catch` blocks, so you can write `catch [System.Management.Automation.ItemNotFoundException]` and it will match correctly. Place more specific exception types before general `catch` blocks.
When should I use $ErrorActionPreference = 'Stop' in a script?
Use it at the top of scripts where any failure should halt execution immediately — deployment scripts, configuration scripts, and data migration scripts where continuing after an error could cause corruption or inconsistent state. It also simplifies `try/catch` usage by making all cmdlet errors catchable without adding `-ErrorAction Stop` to every individual command.
What happens if I set $ErrorActionPreference inside a function?
The change applies only to that function's scope and any child scopes it creates during execution. The calling scope is unaffected. Once the function returns, the caller continues with its own unchanged `$ErrorActionPreference` value. This scoping behaviour makes it safe to change the preference inside a function without side effects on the rest of the script.
What does the Break value do in $ErrorActionPreference?
`Break` instructs PowerShell to enter the interactive debugger at the exact point where an error occurs. This allows you to inspect the call stack, local variables, and execution state at the moment of failure. It is a diagnostic tool intended for development and troubleshooting only and should never be used in production scripts or unattended tasks.
Why should I not use Inquire in automated scripts?
Because `Inquire` pauses execution and presents an interactive prompt to the user whenever an error occurs. In an unattended script — a scheduled task, a CI/CD pipeline, or a remote session — there is no user to respond to the prompt. The script will hang indefinitely with no way to recover automatically.
Does $ErrorActionPreference affect the $Error automatic variable?
Indirectly, through the value chosen. With `Continue` or `Stop`, error records are added to `$Error` normally. With `SilentlyContinue`, errors are still added to `$Error` even though they are not displayed. With `Ignore`, non‑terminating errors are **not** added to `$Error`. Terminating errors suppressed by `Ignore` are still recorded in `$Error`. The `$Error` list holds up to `$MaximumErrorCount` entries, defaulting to 256, with the most recent error at index zero.
How do I override $ErrorActionPreference for just one command?
Use the `-ErrorAction` common parameter (alias `-ea`) on the specific command. For example: `Get-Item 'missing.txt' -ErrorAction SilentlyContinue`. This overrides the global preference for that one invocation only. The scope‑level `$ErrorActionPreference` is unchanged. Note that `-ErrorAction` only works on non‑terminating errors — it cannot suppress errors generated by `$PSCmdlet.ThrowTerminatingError()` inside the cmdlet.
What is $PSNativeCommandUseErrorActionPreference?
A preference variable introduced as stable in PowerShell 7.4 that bridges native command exit codes into PowerShell's error system. When set to `$true`, any native program that exits with a non‑zero code emits a `NativeCommandExitException` non‑terminating error. That error then respects `$ErrorActionPreference`, so setting `Stop` makes native command failures catchable with `try/catch`. Its default value is `$false`.
What is the difference between non-terminating, statement-terminating, and script-terminating errors?
A **non‑terminating** error reports a problem and allows the pipeline and script to continue running. A **statement‑terminating** error stops the current statement but allows the next statement to execute. A **script‑terminating** error unwinds the entire call stack and halts the script completely unless caught by `try/catch` or `trap`. `$ErrorActionPreference = 'Stop'` escalates non‑terminating errors to script‑terminating errors in non‑advanced contexts.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Try, Catch & Finally
Next
Debugging Tools in VSCode
-
The Three Categories of PowerShell Errors
-
All Valid Values for
$ErrorActionPreference -
The Default: Continue
-
Stop – Escalate All Errors to Terminating
- How Escalation Works
-
SilentlyContinue – Suppress the Error Message
-
Ignore – Suppress Without Recording
-
Inquire – Prompt Before Continuing
-
Break – Enter the Debugger
-
Overriding
$ErrorActionPreferencePer Command -
The
$ErrorActionPreferenceAsymmetry with-ErrorAction -
$ErrorActionPreferenceWithtry/catch -
Scope Behaviour
-
Native Command Errors and
$PSNativeCommandUseErrorActionPreference -
Error‑Related Automatic Variables
-
Common Mistakes to Avoid
-
Quick Reference
-
Frequently Asked Questions
