$ErrorActionPreference: the dial behind every error
Most PowerShell errors don’t stop your script — this preference variable decides what happens instead.
Documentation: Microsoft Learn
What the dial does
When a cmdlet hits a problem, it does not always stop. Most errors are non-terminating: the cmdlet reports the problem and the script carries on to the next line. $ErrorActionPreference is the preference variable that decides what happens next. Its default is Continue — print the error and keep going.
The four values
Continue(the default) — write the error and carry on.SilentlyContinue— swallow it. Nothing is printed, but the error is still recorded in$Error. Useful when you have already handled the case yourself; dangerous as a habit, because genuine failures go quiet too.Stop— promote the error to a terminating one. This is the value that makestry/catchwork.Inquire— stop and ask you what to do. Fine at an interactive prompt; never in a script.
Making errors catchable
A catch block only catches terminating errors. Reading a missing file is non-terminating by default, so without -ErrorAction Stop the catch never fires and the script sails past the failure. That is the single most common reason “try/catch doesn’t work”:
The -ErrorAction common parameter overrides the preference for one call — that is the scalpel. Setting $ErrorActionPreference = 'Stop' at the top of a script is the hammer: every cmdlet’s errors become terminating, which is what you want when any failure should abort the run. Going the other way, a global 'SilentlyContinue' is how scripts fail quietly for months — prefer -ErrorAction SilentlyContinue on the one call you truly do not care about.
Try it
Illustrative output · values vary by environment
Example reader
Inspect this page's example
Run supported commands against the practice data, or inspect the example without executing it.
Ready to inspect.
Catches a terminating error and shows that finally runs either way.
Show reference output
Illustrative output · values vary by environment
Caught: Attempted to divide by zero. Finally always runs.
Practise this
Catch a missing service
Request MissingService with -ErrorAction Stop inside try/catch. Return Service unavailable from the catch block.
The sandbox is visible now. Sign in with Google only when you run or check a task; commands and temporary files are discarded when you leave.
Run a command to inspect its result.
Inspect starting data
Supported syntax
Virtual files
Keep going
Next: PowerShell ErrorRecordCheck your understanding
Get-Item reports an error but your catch block does not run. What should you check?
Catch handles terminating errors. Use -ErrorAction Stop on a command when you need its non-terminating error to enter catch. Finally performs cleanup.