How to Use PowerShell ErrorAction? (A Guide with Examples)

This cmdlet handles the action if an error occurs

Reading time icon 3 min. read


Readers help support Windows Report. We may get a commission if you buy through our links. Tooltip Icon

Read our disclosure page to find out how can you help Windows Report sustain the editorial team Read more

Key notes

  • PowerShell ErrorAction is a common parameter that comes into action whenever an error occurs.
  • This guide will discuss PowerShell ErrorAction, its parameters, and how to use them.
PowerShell ErrorAction

When a command fails to execute, PowerShell displays an error based on the value set in ErrorActionPreference.

However, the ErrorAction parameter on PowerShell allows you to specify how to handle terminating errors if a command fails. The options available to handle a command’s execution are Continue, Ignore, Stop, Inquire, SilentlyContinue, and Suspend(works for PowerShell workflow only).

In this guide, we will discuss how to use PowerShell ErrorAction with its variables using examples. Let’s start!

How can I use PowerShell ErrorAction? 

1. Continue 

It is the ErrorAction parameter’s default preference. This action informs the host about the error, and the script continues to execute. You can look at this example to understand better:

  1. Press the Windows key, type PowerShell, and click Run as administrator. Windows PowerShell
  2. Copy and paste the following command and hit Enter to execute: Get-ChildItem C:\nonexistentfolder -ErrorAction Continue
    Write-Output "This line will be executed even if an error occurred."
    Continue PowerShell

2. Ignore

This option doesn’t inform the host about the error and continues the execution of the script. Look at the example to understand:

  1. Open PowerShell using the same method used above.
  2. Type the following command and press Enter: Get-ChildItem C:\nonexistentfolder -ErrorAction Ignore
    Write-Output "This line will be executed even if an error occurred."
    Ignore

3. Stop

If the error has occurred, this action stops the execution of the script. To understand better, check this example:

  1. Open PowerShell with admin rights.
  2. Type the following command and press Enter: Get-ChildItem C:\nonexistentfolder -ErrorAction Stop
    Write-Output "This line will not be executed because an error occurred."
    Stop command

4. Inquire

With this option, when the error occurs, the user gets choices and prompts to determine the appropriate action. Let’s take a look at the example:

  1. First, launch PowerShell with admin rights.
  2. Copy and paste the following command, and press Enter: Get-ChildItem C:\nonexistentfolder -ErrorAction Inquire
    Write-Output "This line will be executed only if you confirm the error message."
    Inquire -powershell erroraction

5. SilentlyContinue

Like the Ignore command, it will not report the error to the host and will continue the execution. However, unlike Ignore, it will add the error to the $Error variable. To understand the action better, let’s take a look at an example:

  1. Open PowerShell with administrator privileges.
  2. Type the following command and press Enter: Get-ChildItem C:\nonexistentfolder -ErrorAction SilentlyContinue
    Write-Output "This line will be executed even if an error occurred."
    Sliently continue -powershell erroraction

So, this is how you can use PowerShell ErrorAction to specify what to do if the command fails to execute. If you have any questions or concerns about the parameter, feel free to let us know in the comments below. We will be happy to help!

More about the topics: PowerShell