PowerShell Genie

$ErrorActionPreference: the dial behind every error

Most PowerShell errors don’t stop your script — this preference variable decides what happens instead.

Lesson 2 min read Windows PowerShell 5.1 and PowerShell 7

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 makes try/catch work.
  • 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”:

1PS> try {
2PS> Get-Item 'C:\nope.txt' -ErrorAction Stop
3PS> } catch {
4PS> "Couldn't read it: $($_.Exception.Message)"
5PS> }

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

1PS> try {
2PS> $result = 10 / 0
3PS> Write-Output "Result: $result"
4PS> }
5PS> catch {
6PS> Write-Output "Caught: $($_.Exception.Message)"
7PS> }
8PS> finally {
9PS> Write-Output 'Finally always runs.'
10PS> }

Example reader

Inspect this page's example

Fictional browser data when supported

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.

    Review the lesson

    Keep going

    Next: PowerShell ErrorRecord

    Check your understanding

    Get-Item reports an error but your catch block does not run. What should you check?

    Genie guide$ErrorActionPreference

    Most PowerShell errors don’t stop your script — this preference variable decides what happens instead.