PowerShell for Windows Admins

May 12 2013   3:20PM GMT

Scripting games–ErrorActionPreference

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I’ve seen a lot of this type of thing in events 1 and 2

$ErrorPref = $ErrorActionPreference
$ErrorActionPreference = “Stop”

Don’t

The default for $ErrorActionPreference is Continue. This means that the error message is shown and the cmdlet attempts to continue. The possible values (from about_Preference_Variables)

Stop: Displays the error message and stops executing.
Inquire: Displays the error message and asks you whether you want to continue.
Continue: Displays the error message and continues executing.
(Default)
SilentlyContinue: No effect. The error message is not displayed and execution continues without interruption.

This preference variable only affects non-terminating errors. A terminating error will still stop processing. Using
$ErrorActionPreference = “Stop”

effectively turns all errors in to terminating errors.
There are times when you want to stop processing and deal with the error such as

try {
some cmdlet
}
catch {
do something
}

In this case use –ErrorAction Stop on the cmdlet to force errors to be terminating. Just makes sure you have the code in place to catch the error.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: