How ErrorAction Parameter Works in PowerShell
If an error occurs in PowerShell, the ErrorActionPreference of PowerShell comes into action. By default, the ErrorActionPreference has a value Continue that shows an error and continues the further execution. For each command, the ErrorActionPreference value is replaced by the option used in the ErrorAction parameter. The ErrorAction can be applied with any cmdlet/Function in the following way:
The working of ErrorAction mainly depends on the <options> specified. The following options are supported by the ErrorAction parameter:
- Continue: This option displays the error and continues the execution.
- Ignore: As the name suggests, it would continue the execution without showing any error.
- Inquire: This option asks the user for confirmation about the action to take.
- SilentlyContinue: This option does not report any error and will continue the execution.
- Stop: This action stops the execution after encountering an error.
- Suspend: This option refers to the PowerShell workflows only. When an error is encountered this option enables you to resume the work where the error occurred.
How to use ErrorAction in PowerShell
This section shows the usage of the ErrorAction parameter in PowerShell:
Using Continue with ErrorAction
As mentioned earlier, the Continue option is a default value of the ErrorActionPreference variable and if it is passed with the ErrorAction parameter then it would return the same error. We have implemented the following two commands:
- First command is executed without the ErrorAction parameter
- The same command is executed with the Continue option of the ErrorAction parameter.
Moreover, above commands are joined with a Write-Host cmdlet for better understanding of the continue option as it continues the execution after throwing an error.
Both the commands return the same output because the default value of ErrorActionPreference is Continue and thus the Continue option of the ErrorAction parameter has no effect on the ErrorActionPreference variable.
> cd D:\ -ErrorAction Continue ; Write-Host "execution continued!!"
Using Inquire with ErrorAction
As the name of the option directs, the inquire option asks you what to do with the command. The command written below executes the Inquire option with the ErrorAction parameter.
We have to choose the action from the following:
Press Y for Yes: This action will proceed you to the execution of the command:
Press A for Yes to All: Press A to have the same action as Yes.
Press H for Halt Command: By hitting H, the command will not be executed after an error and thus does not perform the Write-Host part of the command.
Press S for suspend: Suspending the command will not result in any action.
Using the Ignore option with ErrorAction
The ignore option of the ErrorAction bypasses the error and executes the other part of the command(if provided). For instance, the command provided below implements the “Ignore” option with ErrorAction and the output shows that only the Write-Host part is executed(because it is error free).
Using SilentlyContinue with ErrorAction
The SilentlyContinue option skips the error and the error-free portion of the command is implemented. In the below-stated command, the Write-Error cmdlet is used to generate an error and is exercised with the SilentlyContinue option of ErrorAction parameter.
It is observed that the Ignore option and SilentlyContinue generate the same output. But SilentlyContinue reports the error to the $Error variable of PowerShell, whereas Ignore does not add that error to $Error.
Using Stop with ErrorAction
The stop option as the name suggests stops the command from execution after encountering an error. The following command shows the implementation of the Stop option with ErrorAction.
Conclusion
The PowerShell ErrorAction parameter allows handling the actions if any error occurs. By default, PowerShell operates on the continue option for error handling. However, the ErrorAction operator can be used to change the default option. This write-up briefly explains the usage of ErrorAction with several options supported by this parameter. Each option has a different operating procedure and the ErrorAction parameter replaces the option with default(continue) for each command.