Coding Ref

How to use ErrorAction in PowerShell

How to use ErrorAction in PowerShell

In PowerShell, the -ErrorAction parameter is used to specify the action that should be taken if a non-terminating error occurs.

This parameter is used with cmdlets that support common parameters, such as Write-Output and Get-Item.

ErrorAction parameters

The -ErrorAction parameter has four possible values:

  1. SilentlyContinue: This value specifies that no action should be taken if an error occurs. The error will be ignored and execution will continue.
  2. Stop: This value specifies that execution should be stopped if an error occurs. The error will be displayed and execution will halt.
  3. Continue: This value specifies that execution should continue if an error occurs. The error will be displayed and execution will continue.
  4. Inquire: This value specifies that the user should be prompted to choose an action if an error occurs. The user can choose to continue execution, stop execution, or debug the error.

Examples

Example 1

Here's an example of using the -ErrorAction parameter with the Write-Output cmdlet:

$message = "Hello World"
Write-Output $message -ErrorAction SilentlyContinue

In this example, the Write-Output cmdlet is used to print the value of the $message variable to the console.

The -ErrorAction parameter is set to SilentlyContinue, which specifies that no action should be taken if an error occurs. If an error does occur, it will be ignored and execution will continue.

Example 2

You can also use the -ErrorAction parameter with the Try-Catch block to handle errors in a more controlled way.

Here's an example:

Try {
$message = "Hello World"
Write-Output $message -ErrorAction Stop
}
Catch {
Write-Output "An error occurred"
}

In this example, the Write-Output cmdlet is used inside a Try-Catch block.

The Try block contains the code that may throw an error, and the Catch block contains the code that will be executed if an error occurs.

The -ErrorAction parameter is set to Stop, which specifies that execution should be stopped if an error occurs. If an error does occur, the code in the Catch block will be executed and it will output An error occurred to the console.

ErrorAction with Ignore

You can use the -ErrorAction parameter with the SilentlyContinue value to ignore non-terminating errors. This specifies that no action should be taken if an error occurs, and execution will continue.

Here's an example of using the -ErrorAction parameter with the SilentlyContinue value to ignore errors:

$message = "Hello World"
Write-Output $message -ErrorAction SilentlyContinue

In this example, the Write-Output cmdlet is used to print the value of the $message variable to the console. The -ErrorAction parameter is set to SilentlyContinue, which specifies that no action should be taken if an error occurs. If an error does occur, it will be ignored and execution will continue.

You can also use the -ErrorAction parameter with the SilentlyContinue value inside a Try-Catch block to handle errors in a more controlled way. Here's an example:

Try {
$message = "Hello World"
Write-Output $message -ErrorAction SilentlyContinue
}
Catch {
Write-Output "An error occurred"
}

In this example, the Write-Output cmdlet is used inside a Try-Catch block. The Try block contains the code that may throw an error, and the Catch block contains the code that will be executed if an error occurs. The -ErrorAction parameter is set to SilentlyContinue, which specifies that no action should be taken if an error occurs. If an error does occur, it will be ignored and execution will continue. The code in the Catch block will not be executed.

Conclusion

The -ErrorAction parameter is a useful tool for controlling the behavior of cmdlets in PowerShell when non-terminating errors occur.

You'll also like

Related tutorials curated for you

    What are classes in PowerShell?

    What are the different PowerShell file types?

    How to check for equality in PowerShell

    How to use ErrorAction in PowerShell

    How to get the host or computer name in PowerShell

    How to get processes in PowerShell

    How to use Format-Table in PowerShell

    How to rename a folder in PowerShell

    What is PowerShell Grep?

    How to select an object in PowerShell

    Boolean literals in PowerShell

    How to append content to a file in PowerShell