Powershell

How to use PowerShell ErrorAction parameter

The command line tools are user-dependent where users write commands on the console and thus human error may occur. Usually, if a command fails in PowerShell, an error is thrown based on the value set in ErrorActionPreference. However, PowerShell’s ErrorAction parameter allows you to change the action if an error is encountered. The ErrorAction parameter belongs to PowerShell’s family of common operators and thus supports various options like Continue, SilentlyContinue, Ignore, and many more. In this post, the ErrorAction parameter of PowerShell is described in detail with some practical application on errors.

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:

> <Command> -ErrorAction <options>

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:

  1. First command is executed without the ErrorAction parameter
  2. 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:\ ; Write-Host "execution continued!!"

> cd D:\ -ErrorAction Continue ; Write-Host "execution continued!!"

A screenshot of a computer Description automatically generated with medium confidence

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.

> cd D:\ -ErrorAction Inquire ; Write-Host "execution continued!!"

Graphical user interface, text Description automatically generated with medium confidence

We have to choose the action from the following:

Press Y for Yes: This action will proceed you to the execution of the command:

A screenshot of a computer Description automatically generated with medium confidence

Press A for Yes to All: Press A to have the same action as Yes.

A picture containing chart Description automatically generated

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.

A screenshot of a computer Description automatically generated with medium confidence

Press S for suspend: Suspending the command will not result in any action.

Text Description automatically generated

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).

> cd D:\ -ErrorAction Ignore ; Write-Host "execution continued!!"

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.

> Write-Error "Error encountered" -ErrorAction SilentlyContinue ; Write-Host "execution continued!!"

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.

> Write-Error "Error encountered" -ErrorAction Stop ; Write-Host "execution continued!!"

Text Description automatically generated

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.

About the author

Adnan Shabbir