Using ErrorAction in PowerShell

The ErrorAction Parameter for PowerShell cmdlets is very helpful for… Handling errors 🙂 In this blog post, I will show you the different options it offers and which to use when.

What is ErrorAction?

“Determines how the cmdlet responds to a non-terminating error from the command. This parameter overrides the value of the $ErrorActionPreference variable for non-terminating errors generated by the command or the Write-Error cmdlet.”

Source: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.6#-erroraction

How to use

You can use the -ErrorAction Parameter in a lot of Cmdlets to specify what should happen if the Cmdlet that you are running encounters an error. By default, without specifying the -ErrorAction Parameter, $ErrorActionPreference determines the action to take when an error occurs, which is Continue. This means the error will be displayed, but the script will continue with the next line.

In the chapters below, I will explain the different options of the -ErrorAction Parameter.

Break

When using Break as the value for the -ErrorAction Parameter, the script will stop and display a Debug screen. For example:

In the example above, I used Get-ChildItem and tried to get details about nonexistingfileorfolder. Because I used Break as the ErrorAction value, it started the Debugger to help me. After entering the question mark and hitting Enter, it shows you the available Debugger options:

Now, this is a blog post of its own regarding using the Debugger, but after entering C to continue, it will display the error message:

Note: Useful when debugging scripts, but don’t use it in scheduled tasks, etc.

Continue

Using the Continue value, a script will continue to the next line after displaying the error. (Just like the $ErrorActionPreference, you don’t need to use the Continue value for the -ErrorAction Parameter unless you have altered the $ErrorActionPreference somewhere in your session. For example:

Note: Use this if you’re sure that using continue has no destructive actions on your system 😉

Ignore

Setting Ignore as a value will cause the error to be ignored and not displayed on screen. The error will also not be added to the $Error automatic variable, in which you can see all the errors of your session. For example:

Note: Don’t use this unless you’re sure that ignoring the error from the Cmdlet you’re running can’t cause any issues.

Inquire

When you set Inquire as the value, the Cmdlet will prompt you for what to do when it encounters an error. For example:

In the example above, because it couldn’t find the nonexistingfileorfolder, it asked me what to do. After hitting Enter, it will use the Default Y, show you the error, and continue. Might be useful when your action differs for different inquiries.

Note: Don’t use this in scheduled scripts; it will stop processing and will be stuck on the question.

SilentlyContinue

I used the SilentlyContinue value a lot when I started scripting a long time ago; it would continue without prompting or showing the error screen. (It’s the same as Continue, but silent 😉 )For example:

Note: Use this if you are sure you don’t need the errors to be displayed and if the erors don’t matter 😉

Stop

I often use Stop as the value in my scripts now, especially in Try/Catch scenarios where you want to stop if an error occurs in the Try ScriptBlock. For example:

In the example above, it should have output “File or path found”, but because it ran into an error (Non-existing file or folder) and the -ErrorAction Parameter value was set to Stop, it skipped right to the Catch Scriptblock, and the output was “File or path not found”

Note: Perfect for Try/Catch scenarios where you want to stop processing immediately rather than continue.

Suspend

Setting Suspend to true suspends the workflow and allows it to be resumed when investigated. This only works for Workflows; in other scenarios, this error will be displayed:

Note: More information here https://learn.microsoft.com/en-us/system-center/sma/overview-powershell-workflows?view=sc-sma-2025

Wrapping up

And that’s how you can use ErrorAction 🙂 Have a lovely weekend!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.