PowerTip: Specifying PowerShell Error Actions

PowerTip: Specifying PowerShell Error Actions

  • Comments 9
  • Likes

Summary: Specify Windows PowerShell error actions.

Question: Which command when typed at the beginning of a script will cause Windows PowerShell to ignore errors and continue executing the code?

Answer: $erroractionpreference=SilentlyContinue

Question: How can I ignore errors for a single command instead of for the entire script?

Answer: Use the ErrorAction parameter and specify one of the following values: SilentlyContinue, .

Question: That is a lot of typing. Is there an easier way to specify the ErrorAction for a single command?

Answer: Yes. You can use the parameter alias EA and use a number value for the action. For example, 0 is SilentlyContinue, 1 is Stop, 2 is Continue, 3 is Inquire, or 4 is Ignore.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Helper function to teh rescue:

    0..3|%{'{0} is {1}' -f $_,[enum]::GetName([System.Management.Automation.ActionPreference],$_)}

  • another variant:

    Get-Process MyProc  2>$null

  • @jrv

    you can also:

    [enum]::GetValues([Management.Automation.ActionPreference]) | % {

     "{0} -> {1}" -f ([Management.Automation.ActionPreference]::$_).value__,$_

    }

  • Hi Ed,

    specifiying the error action is extermely important if you want to write scripts, that really run to the end!

    The end may be a special terminating error or the normal end of the script, even if there have been some errors!

    This is important for the acceptance by other users who shouldn't be "threatend" by error messages that they don't understand ( and they can't relate to the cause of an error anyway )

    "$erroractionpreference=SilentlyContinue" is something that should be used very carefully!

    Turning the error check off for a whole script might give you some headaches later on!

    Klaus.  

  • @JRV @Walid Toumi great tips for finding the enumeration values! I also have a function I use to get this information, blogged about it a year or so ago.

    @K_Schulte One of the hardest things I have to do when teaching PowerShell classes to newbies is getting them to READ the error message. Don Jones even suggests changing the RED color to something more friendly! You are absolutely correct that setting everything to silentlycontinue should REALLY REALLY be used with caution.

  • And remember "-ea stop" is useful to convert a non-terminating error into one that will be caught with try...catch when you do want to handle the error.

  • Definitely a handy tip have added to favorites. Would have been nice if it had bothered to include and confirm the other four number values though. So 0 is SilentlyContinue, are the other four in order, so Stop = 1, Continue = 2, Inquire = 3 and Ignore = 4?

  • @RJCox you are absolutely correct. I often change the ErrorActionPreference to Stop so I can use Try / Catch error handling to work with non-terminating errors. Great pointer. Thanks.

  • @Keith Langmead Yeah, I thought about adding the enumeration values but decided against it -- it was an intended decision because it is a "quick tip" and not a full blown article. I wrote a Hey Scripting Guy blog article a while back that includes a function that retrieves enumeration names and values from enum types. In fact, I use that function to this day and have it in my profile. Here is the link: blogs.technet.com/.../hey-scripting-guy-weekend-scripter-enumerations-and-values.aspx to the article. Also, I figured that the community would also chip in and list the enum values if there was sufficient question ... and both JRV and Walid wrote code that lists the enum values and names. I did, however, go ahead and update the post based upon your recommendation, because in retrospect it does seem like an omission. Thanks for the suggestion.