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
.
The -ErrorAction
parameter has four possible values:
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.
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.
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.
The -ErrorAction
parameter is a useful tool for controlling the behavior of cmdlets in PowerShell when non-terminating errors occur.
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