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

This cmdlet handles the action if an error occurs

by Srishti Sisodia
Srishti Sisodia
Srishti Sisodia
Author
Srishti is an avid writer with a keen interest in technology. She has spent the past ten years of her career researching and writing troubleshooting articles for Windows... read more
Reviewed by Alex Serban
Alex Serban
Alex Serban
Windows Server & Networking Expert
After moving away from the corporate work-style, Alex has found rewards in a lifestyle of constant analysis, team coordination and pestering his colleagues. Holding an MCSA Windows Server... read more
Affiliate Disclosure
  • 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

XINSTALL BY CLICKING THE DOWNLOAD FILE
To fix various PC problems, we recommend Restoro PC Repair Tool:
This software will repair common computer errors, protect you from file loss, malware, hardware failure and optimize your PC for maximum performance. Fix PC issues and remove viruses now in 3 easy steps:

  1. Download Restoro PC Repair Tool that comes with Patented Technologies (patent available here).
  2. Click Start Scan to find Windows issues that could be causing PC problems.
  3. Click Repair All to fix issues affecting your computer's security and performance
  • Restoro has been downloaded by 0 readers this month.

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!

Still having issues? Fix them with this tool:

SPONSORED

If the advices above haven't solved your issue, your PC may experience deeper Windows problems. We recommend downloading this PC Repair tool (rated Great on TrustPilot.com) to easily address them. After installation, simply click the Start Scan button and then press on Repair All.

This article covers:Topics: