If you are having prior software development experience, for exception handling we generally use PowerShell Try-Catch-Finally block. PowerShell also support try-catch-finally block to handle or respond to terminating errors in PowerShell script.
In PowerShell 2.0, common parameters related to error handling available are: -ErrorAction and -ErrorVariable
In this blog post, I will explain you about using –ErrorAction and –ErrorVariable parameter in PowerShell with examples.
PowerShell ErrorAction Parameter
The PowerShell ErrorAction parameter allows you to specify how to respond or handle terminating errors or actions to take if command fails.
ErrorAction parameter has below options available to handle execution of command if error raised
Let’s understand each of ErrorAction PowerShell options with examples
ErrorAction PowerShell parameter, it basically override the $ErrorActionPreference in Windows PowerShell.
$ErrorActionPreference variable responds to which action to take in case of error raised.
using PowerShell ErrorAction parameter, it default uses continue preference. It means that in the event of error raised, its output will be written to host and script will be able to continue.
Lets consider an example to stop process RabbitMQ and handle any error produced using ErrorAction PowerShell parameter
PS C:\> Stop-Process -Name RabbitMQ -ErrorAction Continue; Write-Host "Execute second command";
In the above example, Stop-Process cmdlet takes RabbitMQ process name and try to stop it. As it cannot find a process with given name, it throws exception.
Error raised is output to host and using –ErrorAction continue option, it continue with execution for second command.
It converts terminating errors to non-terminating errors to continue with execution and write output to host as given below
Do you know: How to use cat command in windows!
PowerShell -ErrorAction Ignore
PowerShell ErrorAction Ignore option does not produce any error message and write any error output on host and continue with execution.
-ErrorAction Ignore option no errors will be updated in PowerShell $Error variable (automatic variable).
Lets consider same example to stop RabbitMQ process. We will use -ErrorAction Ignore option to ignore any error message and continue with execution of second command.
PS C:\> Stop-Process -Name RabbitMQ -ErrorAction Ignore; Write-Host "Execute second command";
Output of above command is as below
PowerShell Tip: How to add newline to string or variable?
PowerShell ErrorAction Inquire option produce an error and prompt user to confirm actions to take
a. Yes b. Yes to All c. Halt Command d. Suspend
Based on users action, it decide to continue with execution or halt or suspend it.
Let’s consider above example to stop RabbitMQ process using Stop-Process. We will use -ErrorAction Inquire option to let user to confirm action in the case of error produced
PS C:\>> Stop-Process -Name RabbitMQ -ErrorAction Inquire; Write-Host "Execute second command";
Output of the above command with user selection as Yes
If the user select Halt Command, it will write error message on Host and halt further execution. If the user select Suspend option, it will suspend the current execution.
Cool Tip: Replace text in string using PowerShell!
PowerShell -ErrorAction SilentlyContinue
–ErrroAction SilentlyContinue in PowerShell silently continue with the execution of code if the part of code does not work or have non-terminating errors.
SilentlyContinue option silence non-terminating errors and continue execution. Error will updated in PowerShell Error variable (automatic variable).
Let’s consider example to stop process name as RabbitMQ using Stop-Process. We will use -ErrorAction SilentlyContinue option to silence non-terminating errors and execution of second command.
PS C:\>>> Stop-Process -Name RabbitMQ -ErrorAction SilentlyContinue; Write-Host "Execute second command";
Output of the above command is as below
Cool Tip: Get-ChildItem cmdlet – Search for files in PowerShell!
PowerShell ErrorAction Stop option display error message on host and stop further execution of code. It terminates the code.
Lets consider similar example given above to find process name as RabbitMQ. We will use -ErrorAction Stop option to check execution.
PS C:\>>> Stop-Process -Name RabbitMQ -ErrorAction Stop; Write-Host "Execute second command";
The output of the above command is as below
PowerShell -ErrorAction Suspend option available in PowerShell workflows. If error raised in command, workflows will get suspended.
Cool Tip: Best way to use PowerShell Get-ChildItem cmdlet!
PowerShell ErrorVariable Parameter
When you run PowerShell command, in the event of error raised, error will added into PowerShell $Error automatic variable.
Using PowerShell ErrorVariable parameter, you can specify your own variable name to store error message.
PowerShell ErrorVariable will overwrite error message value to variable name. If you want to append error in variable name, use + in front of variable name. It will append error message.
Let’s consider an example to illustrate -ErrroVariable in PowerShell. We will use same example given above to stop process name as RabbitMQ. As command not able to find process, it will raise error.
Using -ErrorVariable PowerShell, we will store error message in processError variable name as given below
PS C:\>>> Stop-Process -Name RabbitMQ -ErrorVariable processError;
In the above command, it will add error message on PowerShell Error variable as well as on to processvariable variable.
If we print processError variable name, it will write error output on host as given below
Cool Tip: Get-FileHash cmdlet – Get MD5 checksum in PowerShell!
You can make the use of -ErrorAction and -ErrorVariable together to silent non-terminating errors and add error message in variable to take further action
Using the same example given above with -ErrorAction SilentlyContinue option and -ErrorVariable to store error message in
PS C:\>>> Stop-Process -Name RabbitMQ -ErrorAction SilentlyContinue -ErrorVariable processError; PS C:\>>> $processError
In the above code, SilentlyContinue option in PowerShell silent non-terminating errors and -ErrorVariable parameter add error message to processError variable as given below
Cool Tip: Read more about PowerShell $PSScriptRoot Automatic Variable!
I hope you may have like above detailed article about PowerShell ErrorAction parameter and -ErrorVariable PowerShell parameter.
Using different options available with -ErrorAction, we can handle error message in code.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on ShellGeek home page.