Skip to content

ErrorActionPreference

$ErrorActionPreference is a built‑in PowerShell variable that controls how PowerShell responds to non‑terminating errors. By changing its value, you can decide whether PowerShell should continue, stop, ignore, or ask for confirmation when an error occurs.

This setting affects all commands in the current scope unless overridden by a cmdlet’s -ErrorAction parameter.


1. What $ErrorActionPreference Controls

PowerShell cmdlets normally generate non‑terminating errors, meaning the script continues running even if something fails.

$ErrorActionPreference changes this behavior globally.

Common values:

Value Meaning
Continue Default. Show the error and continue.
Stop Treat all errors as terminating.
SilentlyContinue Suppress the error message and continue.
Inquire Ask the user what to do when an error occurs.
Ignore Ignore the error completely (no message, no record).

2. Default Behavior: Continue

The default value is:

$ErrorActionPreference

Output:

Continue

This means:

  • Errors are displayed
  • Execution continues

Example:

Remove-Item "C:\\NoSuchFile.txt"
"Still running"

Output:

Remove-Item : Cannot find path...
Still running

3. Forcing All Errors to Stop: Stop

Setting:

$ErrorActionPreference = "Stop"

Now any error becomes a terminating error.

Example:

$ErrorActionPreference = "Stop"

Remove-Item "C:\\NoSuchFile.txt"
"Still running"

Execution stops before "Still running".

This is essential when you want to use try/catch without adding -ErrorAction Stop to every command.


4. Hiding Errors: SilentlyContinue

$ErrorActionPreference = "SilentlyContinue"

This suppresses error messages but still records them in $Error.

Example:

$ErrorActionPreference = "SilentlyContinue"

Remove-Item "C:\\NoSuchFile.txt"
"Still running"

Output:

Still running

No error message is shown.


5. Completely Ignoring Errors: Ignore

$ErrorActionPreference = "Ignore"

This:

  • Suppresses the error
  • Does not record it in $Error
  • Does not trigger try/catch

Use this only when you truly do not care about the error.


6. Asking the User What to Do: Inquire

$ErrorActionPreference = "Inquire"

When an error occurs, PowerShell prompts:

Confirm
[?] Continue, Suspend, or Quit?

This is useful in interactive sessions, not in scripts.


7. Overriding $ErrorActionPreference Per Command

You can override the global setting using -ErrorAction.

Example:

$ErrorActionPreference = "Continue"

Remove-Item "C:\\NoSuchFile.txt" -ErrorAction Stop

Even though the global preference is Continue, this command stops on error.


8. Using $ErrorActionPreference With try/catch

To catch errors without adding -ErrorAction Stop everywhere:

$ErrorActionPreference = "Stop"

try {
    Remove-Item "C:\\NoSuchFile.txt"
}
catch {
    "Caught: $($_.Exception.Message)"
}

Because all errors are terminating, catch works automatically.


9. Scope Behavior

$ErrorActionPreference follows PowerShell’s scoping rules:

  • Changing it inside a function affects only that function
  • Changing it at the script level affects the whole script
  • Changing it globally affects the entire session

Example:

function Test {
    $ErrorActionPreference = "SilentlyContinue"
    Remove-Item "C:\\NoSuchFile.txt"
}

Test

Outside the function, the preference remains unchanged.


10. Summary

Setting Behavior
Continue Show error, continue (default)
Stop Treat all errors as terminating
SilentlyContinue Hide error, continue
Ignore Hide error completely
Inquire Ask user what to do

$ErrorActionPreference is a powerful tool for controlling error behavior across your entire script or session, especially when combined with try/catch.