How to Use PowerShell ErrorAction? (A Guide with Examples)
This cmdlet handles the action if an error occurs
3 min. read
Updated on
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.
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:
- Press the Windows key, type PowerShell, and click Run as administrator.
- 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."
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:
- Open PowerShell using the same method used above.
- 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."
3. Stop
If the error has occurred, this action stops the execution of the script. To understand better, check this example:
- Open PowerShell with admin rights.
- 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."
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:
- First, launch PowerShell with admin rights.
- 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."
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:
- Open PowerShell with administrator privileges.
- 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."
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!
User forum
0 messages