Problem
You want to handle warnings, errors, and terminating errors generated by scripts or other tools that you call.
Solution
To control how your script responds to warning messages, set the $warningPreference variable. In this example, to ignore them: $warningPreference = "SilentlyContinue"
To control how your script responds to nonterminating errors, set the $errorActionPreference variable. In this example, to ignore them:
$errorActionPreference = "SilentlyContinue" To control how your script responds to terminating errors, use the trap statement. In this example, to output a message and continue with the script:
trap [DivideByZeroException] { "Don't divide by zero!"; continue }
Discussion
PowerShell defines several preference variables that help you control how your script reacts to warnings, errors, and terminating errors. As an example of these error management techniques, consider the following script:
############################################################################## ## ## GetWarningsAndErrors.ps1 ## ## Demonstrates the functionality of the WriteWarning, WriteError, and throw ## statements ## ##############################################################################
WriteWarning "Warning: About to generate an error" WriteError "Error: You are running this script" throw "Could not complete operation."
You can now see how a script might manage those separate types of errors:
PS >$warningPreference = "Continue" PS >GetWarningsAndErrors.ps1 WARNING: Warning: About to generate an error .. GetWarningsAndErrors.ps1 : Error: You are
running this script At line:1 char:27
+
GetWarningsAndErrors.ps1 <<<< Could not complete operation. At .. GetWarningsAndErrors.ps1:12 char:6
+
throw <<<< "Could not complete operation."
Once you modify the warning preference, the original warning message gets suppressed:
PS >$warningPreference = "SilentlyContinue" PS >GetWarningsAndErrors.ps1 .. GetWarningsAndErrors.ps1 : Error: You are
running this script At line:1 char:27
+
GetWarningsAndErrors.ps1 <<<< Could not complete operation. At .. GetWarningsAndErrors.ps1:12 char:6
+
throw <<<< "Could not complete operation."
When you modify the error preference, you suppress errors and exceptions, as well:
PS >$errorActionPreference = "SilentlyContinue" PS >GetWarningsAndErrors.ps1 PS >
An addition to the $errorActionPreference variable, all cmdlets allow you to specify your preference during an individual call:
PS >$errorActionPreference = "Continue" PS >GetChildItem IDoNotExist GetChildItem : Cannot find path '...\IDoNotExist' because it does not exist. At line:1 char:14
+ GetChildItem <<<< IDoNotExist PS >GetChildItem IDoNotExist ErrorAction SilentlyContinue PS >
If you reset the error preference back to Continue, you can see the impact of a trap statement. The message from the WriteError call makes it through, but the exception does not:
PS >$errorActionPreference = "Continue" PS >trap { "Caught an error"; continue }; GetWarningsAndErrors .. GetWarningsAndErrors.ps1 : Error: You are
running this script At line:1 char:61
+ trap { "Caught an error"; continue }; GetWarningsAndErrors <<<< Caught an error