{"id":51063,"date":"2010-03-08T00:01:00","date_gmt":"2010-03-08T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/03\/08\/hey-scripting-guy-how-can-i-use-erroractionpreference-to-control-cmdlet-handling-of-errors\/"},"modified":"2010-03-08T00:01:00","modified_gmt":"2010-03-08T00:01:00","slug":"hey-scripting-guy-how-can-i-use-erroractionpreference-to-control-cmdlet-handling-of-errors","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-use-erroractionpreference-to-control-cmdlet-handling-of-errors\/","title":{"rendered":"Hey, Scripting Guy! How Can I Use $ErrorActionPreference to Control Cmdlet Handling of Errors?"},"content":{"rendered":"<p class=\"MsoNormal\"><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a>&nbsp;<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/p>\n<p class=\"MsoNormal\">Hey, Scripting Guy! One thing I liked about using VBScript is that I could add <b>On Error Resume Next<\/b> to the top of a script, and all my problems with the script went away. It was like magic, and I used it to fix many of my scripts. I really miss this capability in Windows PowerShell. <\/p>\n<p class=\"MsoNormal\">&#8212; MW<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">Hello MW, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. It is Monday morning. Generally, I love Mondays. Although if the truth were told, they are not that much different from Saturdays or Thursdays. A day of scripting is after all a day of scripting, and that means they are all good! Those of you who follow the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a> know that you are quite likely to find either Craig or me hanging out over the weekend. This weekend was a bit different. I have been in contact with a professional woodworker who studied with <a href=\"http:\/\/en.wikipedia.org\/wiki\/James_Krenov\"><font face=\"Segoe\">James Krenov<\/font><\/a> at the <a href=\"http:\/\/en.wikipedia.org\/wiki\/College_of_the_Redwoods\"><font face=\"Segoe\">College of the Redwoods<\/font><\/a>. We sent e-mail back and forth, and he agreed to give me private lessons in his studio. It made for an excellent weekend, but because his studio is several hundred miles away, I am a bit tired this morning. In fact, this morning is an <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/08\/hey-scripting-guy-september-8-2009.aspx\"><font face=\"Segoe\">unplanned coffee morning<\/font><\/a>. I have my French press sitting beside my Zune HD, and I am sipping a cup of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Peaberry\"><font face=\"Segoe\">Peaberry<\/font><\/a> <a href=\"http:\/\/en.wikipedia.org\/wiki\/Kona_coffee\"><font face=\"Segoe\">Kona coffee<\/font><\/a> from Kauai, which has a rich, nutty taste with hints of cherry and nutmeg. Whereas I normally put a cinnamon stick in a cup of coffee (a habit I picked up while I was teaching VBScript in Lisbon, Portugal), I feel to do so with this excellent coffee would almost be an insult. The same is true of loading it down with sugar and milk. Therefore, I drink this coffee unadorned. Speaking of Kauai, in addition to having excellent coffee, there is great scuba diving there. The following photograph is one I took during a trip there. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Photograph taken in the waters off Kauai\" alt=\"Photograph taken in the waters off Kauai\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0308\/hsg-03-08-10-01.jpg\" width=\"600\" height=\"450\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">MW, you can achieve something similar to <b>On Error Resume Next<\/b> by assigning the string <b>SilentlyContinue<\/b> to the <b>$ErrorActionPreference<\/b> automatic variable. The reason I say it is similar is that it does not hide all errors. For example, if I try to create an object that does not exist, such as the infamous <b>foo<\/b>, Windows PowerShell generates an error (in spite of all the examples of this in various programming books). The error seen here tells you it cannot find the type for <b>foo<\/b>, and that the assembly containing this type is not loaded:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; New-Object foo<br \/>New-Object : Cannot find type [foo]: make sure the assembly containing this type is loaded.<br \/>At line:1 char:11<br \/>+ New-Object &lt;&lt;&lt;&lt;<span>&nbsp; <\/span>foo<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>+ CategoryInfo<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: InvalidType: (:) [New-Object], PSArgumentException<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">To hide this error, set the value of the automatic variable <b>$ErrorActionPreference<\/b> to <b>SilentlyContinue<\/b>, as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; $ErrorActionPreference = &#8220;silentlycontinue&#8221;<br \/>PS C:&gt; New-Object foo<br \/>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">This command does not suppress all errors. For example, if I generate a divide-by-zero error, an error appears. Windows PowerShell generates the error regardless of the setting for the <b>$ErrorActionPreference<\/b> automatic variable. The following code illustrates this:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; 1\/0<br \/>Attempted to divide by zero.<br \/>At line:1 char:3<br \/>+ 1\/ &lt;&lt;&lt;&lt; 0<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>+ CategoryInfo<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: NotSpecified: (:) [], ParentContainsErrorRecordException<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>+ FullyQualifiedErrorId : RuntimeException<\/p>\n<p>PS C:&gt; $ErrorActionPreference = silentlycontinue<br \/>The term &#8216;silentlycontinue&#8217; is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.<br \/>At line:1 char:42<br \/>+ $ErrorActionPreference = silentlycontinue &lt;&lt;&lt;&lt;<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>+ CategoryInfo<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: ObjectNotFound: (silentlycontinue:String) [], CommandNotFoundException<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>+ FullyQualifiedErrorId : CommandNotFoundException<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The difference in behavior is because the <b>$ErrorActionPreference<\/b> automatic variable controls reporting from cmdlets and not system errors. In addition, it is i\nmportant to remember that the <b>$ErrorActionPreference<\/b> automatic variable is only valid for <b>&ndash;not<\/b> terminating errors. So what is the difference between a terminating error and a nonterminating error? A terminating error is raised by a cmdlet (by calling the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd878319(VS.85).aspx\"><font face=\"Segoe\">ThrowTerminatingError<\/font><\/a> method) when the error is so bad that the cmdlet would not be able to continue processing the pipeline or in some cases the entire script. Generally, these will be syntax errors. <\/p>\n<p class=\"MsoNormal\">A nonterminating error is one that is not so bad that it would halt the execution of the cmdlet or the script. For example, attempting to read a text file that does not exist does not mean that the <b>Get-Content<\/b> cmdlet will stop working. If you give it the path to a file that does exist, it will work just fine as shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-Content foo<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Get-Content : Cannot find path &#8216;C:foo&#8217; because it does not exist.<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">At line:1 char:12<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">+ Get-Content &lt;&lt;&lt;&lt;<span>&nbsp; <\/span>foo<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;&nbsp;&nbsp; <\/span>+ CategoryInfo<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: ObjectNotFound: (C:foo:String) [Get-Content], ItemNotFoundException<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;&nbsp;&nbsp; <\/span>+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><\/p>\n<p><font face=\"Lucida Sans Typewriter\">&nbsp;<\/font><\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-Content C:fsoComputers.txt<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">win7-pc<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">hyperv<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Therefore, you may wish to hide the error message from the first command by using the <b>$ErrorActionPreference<\/b> automatic variable. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; $ErrorActionPreference = &#8220;SilentlyContinue&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-Content foo<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-Content C:fsoComputers.txt<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">win7-pc<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">hyperv<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">You may wish to see the error messages, perhaps because you are writing a script and you wish to ensure that all the dependencies for the script are in place. When I was teaching VBScript classes all over the world, I would often have students raise their hands during lab time, and state that their script was not working properly, but they were not seeing any errors. I did not even need to leave my desk; I simply told them to comment out the <b>On Error Resume Next<\/b> line of code. One of the unfortunate things about many of the scripts seen on the Script Center is the constant use of <b>On Error Resume Next<\/b>, because it hides errors and actually encourages bad practices. I understand why it is there, because you do not want beginners to face many errors when they are taking their first steps into scripting. But at some point, you need to turn it off and fix the script. There is no problem using the command, if you know why the command is used.<\/p>\n<p class=\"MsoNormal\">By default, the value of <b>$ErrorActionPreference<\/b> is set to <b>Continue<\/b> which means that an error will be displayed, but the script (or command) will attempt to continue. There are in fact four values that can be assigned to the <b>$ErrorActionPreference<\/b> variable: <b>SilentlyContinue<\/b>, <b>Continue<\/b>, <b>Stop<\/b>, and <b>Inquire<\/b>. <\/p>\n<p class=\"MsoNormal\">If the value of <b>$ErrorActionPreference<\/b> is set to <b>Stop<\/b>, a script will halt execution at the failed command&ndash;-even if the subsequent commands would have worked. The following image illustrat\nes this concept.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of results of $ErrorActionPreference set to Stop\" alt=\"Image of results of $ErrorActionPreference set to Stop\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0308\/hsg-03-08-10-02.jpg\" width=\"600\" height=\"421\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">If I would like the user of my script to be prompted for the correct behavior of the error action, I can use the <b>inquire<\/b> setting. When running from within the Windows PowerShell ISE, a dialog box displays and asks you what action to take. This is shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Confirm dialog box\" alt=\"Image of Confirm dialog box\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0308\/hsg-03-08-10-03.jpg\" width=\"417\" height=\"120\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">The dialog box is a bit confusing, but the title of the dialog box is <b>Confirm<\/b>. Therefore, it states the file does not exist, and it is asking you to confirm if you want to continue processing the script, suspend the script, or halt the command. If you click <strong>Yes<\/strong>, the error will be displayed, and the script continues running. This is shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of error displayed as script continues running\" alt=\"Image of error displayed as script continues running\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0308\/hsg-03-08-10-04.jpg\" width=\"600\" height=\"421\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">When you run the script from the Windows PowerShell shell (as opposed to the ISE), you are prompted inside the shell with a <b>Confirm<\/b> prompt as shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of script run from Windows PowerShell shell\" alt=\"Image of script run from Windows PowerShell shell\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0308\/hsg-03-08-10-05.jpg\" width=\"600\" height=\"248\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">MW, that is all there is to using the <b>$ErrorActionPreference<\/b> variable to control cmdlet handling of errors. Error Handling Week will continue tomorrow. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><font face=\"Segoe\">Official Scripting Guys Forum<\/font><\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/b><\/p>\n<p><b><span><\/span><\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; Hey, Scripting Guy! One thing I liked about using VBScript is that I could add On Error Resume Next to the top of a script, and all my problems with the script went away. It was like magic, and I used it to fix many of my scripts. I really miss this capability [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[68,51,3,4,45],"class_list":["post-51063","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-error-handling","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; &nbsp; Hey, Scripting Guy! One thing I liked about using VBScript is that I could add On Error Resume Next to the top of a script, and all my problems with the script went away. It was like magic, and I used it to fix many of my scripts. I really miss this capability [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=51063"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51063\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=51063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}