{"id":51053,"date":"2010-03-09T00:01:00","date_gmt":"2010-03-09T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/03\/09\/hey-scripting-guy-how-can-i-use-the-erroraction-preference-parameter\/"},"modified":"2010-03-09T00:01:00","modified_gmt":"2010-03-09T00:01:00","slug":"hey-scripting-guy-how-can-i-use-the-erroraction-preference-parameter","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-use-the-erroraction-preference-parameter\/","title":{"rendered":"Hey, Scripting Guy! How Can I Use the &#8211;ErrorAction Preference Parameter?"},"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 class=\"MsoNormal\"><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\">Hey, Scripting Guy! I enjoyed reading your blog post yesterday about using <b>$ErrorActionPreference<\/b> to control the behavior of a script. One thing I never liked about using &ldquo;On Error Resume Next&rdquo; from VBScript was that it seemed to be an all-or-nothing type of proposition. There are certain errors I do not need to see, but then again, there are some errors I definitely must see. Is there another approach I can take to using <b>$ErrorActionPreference<\/b>?<\/p>\n<\/p>\n<p class=\"MsoNormal\">&#8212; TS<\/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 TS, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. Today has been a crazy day, and it will continue being crazy until into the night. I am between meetings right now. I did a Live Meeting presentation to the <a href=\"http:\/\/www.okcdg.org\/?p=194\"><font face=\"Segoe\">Oklahoma City Developers Group<\/font><\/a> this morning, and I will have another presentation this evening. Immediately after I finished this morning&rsquo;s presentation, Craig and I had a conference call with a group inside Microsoft that is investigating reaching out to the community, and they wanted to talk to The Scripting Guys. In addition to the three presentations today, two other meetings canceled. One problem with canceled meetings is they have a way of cropping back up on your calendar. I am taking advantage of one of those canceled meetings to check the <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\"><span><font face=\"Segoe\">scripter@microsoft.com<\/font><\/span><\/a> e-mail account. <\/p>\n<p class=\"MsoNormal\">One of the cool things you can do with Windows PowerShell is use the <b>&ndash;ErrorAction<\/b> parameter. It is one of the standard parameters, and is therefore available on all cmdlets. This allows you to specify a custom error action for each cmdlet or each line of code in your script. In the code seen here, the first line of code informs you of an error if it occurs while retrieving the content of the c:fsocomputers.txt file. If an error occurs, after informing you of the problem, the script continues to the next line. The <b>erroraction<\/b> for the second line of code is stop. The script will halt if the <b>Get-Content<\/b> cmdlet is unable to find <b>foo<\/b>. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Get-Content -ErrorAction continue -Path C:fsoComputers.txt<br \/>Get-Content -ErrorAction stop -Path foo<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When the script runs, the output appears that is shown in the following image. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of script output\" alt=\"Image of script output\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0309\/hsg-03-09-10-01.jpg\" width=\"600\" height=\"423\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">The <b>&ndash;ErrorAction<\/b> parameter works in exactly the same manner as the <b>$ErrorActionPreference<\/b> automatic variable. The <b>$ErrorActionPreference<\/b> automatic variable was discussed in <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2010\/03\/08\/hey-scripting-guy-march-8-2010.aspx\"><span>yesterday&rsquo;s Hey, Scripting Guy! Blog post<\/span><\/a>.<\/p>\n<p class=\"MsoNormal\">If you are working interactively from within the Windows PowerShell console, you might not want to type <b>&ndash;ErrorAction<\/b> every time you wish to specify a different error action level. Even with tab expansion, it can be a bit annoying. To work around that problem, you can use the parameter alias, <b>ea<\/b>, as shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-WmiObject -Class win32_bios -EA silentlyContinue<\/p>\n<p>SMBIOSBIOSVersion : A05<br \/>Manufacturer<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Dell Inc.<br \/>Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Phoenix ROM BIOS PLUS Version 1.10 A05<br \/>SerialNumber<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: BDY91L1<br \/>Version<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: DELL<span>&nbsp;&nbsp; <\/span>&#8211; 15<\/p>\n<p>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">With tab expansion, the big problem is not typing the <b>&ndash;ErrorAction<\/b> parameter name, but supplying the value for the parameter. One thing to keep in mind is that the <b>&ndash;ErrorAction<\/b> parameter expects a string, and therefore you do not have to supply the value inside quotation marks. You can leave out the quotation marks, and it works just fine. Of course, if you forget and put them in, it does not affect the outcome. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-WmiObject -Class win32_bios -EA &#8220;silentlyContinue&#8221;<\/p>\n<p>SMBIOSBIOSVersion : A05<br \/>Manufacturer<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Dell Inc.<br \/>Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Phoenix ROM BIOS PLUS Version 1.10 A05<br \/>SerialNumber<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: BDY91L1<br \/>Version<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;nb\nsp;&nbsp; <\/span>: DELL<span>&nbsp;&nbsp; <\/span>&#8211; 15<\/p>\n<p>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">On the other hand, you must spell the parameter correctly. If you do not, the error that is generated will tell you the allowable parameters. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-WmiObject -Class win32_bios -EA silentlyContinuee<br \/>Get-WmiObject : Cannot bind parameter &#8216;ErrorAction&#8217;. Cannot convert value &#8220;silentlyContinuee&#8221; to type &#8220;System.Management.Automation.ActionPreference&#8221; due to invalid enumeration values. Specify one of the following enumeration values and try again. The possible enumeration values are &#8220;SilentlyContinue, Stop, Continue, Inquire&#8221;.<br \/>At line:1 char:36<br \/>+ Get-WmiObject -Class win32_bios -EA &lt;&lt;&lt;&lt;<span>&nbsp; <\/span>silentlyContinuee<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>+ CategoryInfo<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: InvalidArgument: (:) [Get-WmiObject], ParameterBindingException<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetWmiObjectCommand<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Notice that the error message tells you the allowed enumeration values: <b>SilentlyContinue<\/b>, <b>Stop<\/b>, <b>Continue<\/b>, and <b>Inquire<\/b>. The values you can supply for the <b>&ndash;ErrorAction<\/b> parameter are members of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.management.automation.actionpreference(VS.85).aspx\"><font face=\"Segoe\">System.Management.Automation.ActionPreference<\/font><\/a> .NET Framework class. The enumeration and association values are seen in Table 1.<\/p>\n<p class=\"TableNum-Title\"><strong>Table 1<\/p>\n<p><\/strong><\/p>\n<table class=\"MsoNormalTable\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"144\">\n<p class=\"TableHead\">Enumeration<\/p>\n<\/td>\n<td valign=\"top\" width=\"48\">\n<p class=\"TableHead\">Value<\/p>\n<\/td>\n<td valign=\"top\" width=\"433\">\n<p class=\"TableHead\">Description<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"144\">\n<p class=\"TableText\">SilentlyContinue<\/p>\n<\/td>\n<td valign=\"top\" width=\"48\">\n<p class=\"TableText\"><span>&nbsp;<\/span>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"433\">\n<p class=\"TableText\"><span>&nbsp;<\/span>The Windows PowerShell runtime will continue processing without notifying the user that an action has occurred. <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"144\">\n<p class=\"TableText\">Stop<\/p>\n<\/td>\n<td valign=\"top\" width=\"48\">\n<p class=\"TableText\"><span>&nbsp;<\/span>1<\/p>\n<\/td>\n<td valign=\"top\" width=\"433\">\n<p class=\"TableText\"><span>&nbsp;<\/span>The Windows PowerShell runtime will stop processing when an action occurs.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"144\">\n<p class=\"TableText\">Continue<\/p>\n<\/td>\n<td valign=\"top\" width=\"48\">\n<p class=\"TableText\"><span>&nbsp;<\/span>2<\/p>\n<\/td>\n<td valign=\"top\" width=\"433\">\n<p class=\"TableText\">The Windows PowerShell runtime will continue processing and notify the user that an action has occurred. <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"144\">\n<p class=\"TableText\">Inquire<\/p>\n<\/td>\n<td valign=\"top\" width=\"48\">\n<p class=\"TableText\"><span>&nbsp;<\/span>3<\/p>\n<\/td>\n<td valign=\"top\" width=\"433\">\n<p class=\"TableText\"><span>&nbsp;<\/span>The Windows PowerShell runtime will stop processing and ask the user how it should proceed. <\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">An old post on the <a href=\"http:\/\/blogs.msdn.com\/powershell\/archive\/2008\/03\/29\/erroraction-silentlycontinue-ea-0.aspx\"><font face=\"Segoe\">Windows PowerShell team blog<\/font><\/a> indicates you can use the enumeration values directly. This is a great solution when trying to type short commands from the Windows PowerShell console. Using the alias <b>gc<\/b> for <b>Get-Content<\/b>, leaving out the positional argument path, and using the alias <b>ea<\/b> for the <b>&ndash;ErrorAction<\/b> parameter, and the enumeration value <b>0<\/b> for <b>SilentlyContinue<\/b> results in a very short command. This example is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; gc foo -ea 0<br \/>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">If you want to have the command inquire about the action, use the <b>&ndash;ErrorAction<\/b> enumeration value of 3 as shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; gc foo -ea 3<\/p>\n<p>Confirm<br \/>Cannot find path &#8216;C:foo&#8217; because it does not exist.<br \/>[Y] Yes<span>&nbsp; <\/span>[A] Yes to All<span>&nbsp; <\/span>[H] Halt Command<span>&nbsp; <\/span>[S] Suspend<span>&nbsp; <\/span>[?] Help (default is &#8220;Y&#8221;): y<br \/>Get-Content : Cannot find path &#8216;C:foo&#8217; because it does not exist.<br \/>At line:1 char:3<br \/>+ gc &lt;&lt;&lt;&lt;<span>&nbsp; <\/span>foo -ea 3<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>+ CategoryInfo<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: ObjectNotFound: (C:foo:String) [Get-Content], ItemNotFoundException<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand<\/p>\n<p>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When writing a script, I do not recommend using aliases and enumeration values directly because it makes the script very difficult to read, which in turn makes the script harder to troubleshoot. However, when working interactively from the Windows PowerShell prompt, it is perfectly acceptable to use the enumeration values. In fact, you can also use partial parameters as well, as long as you type enough of the parameter name to make it unique. In the example seen here, where <b>&ndash;com<\/b> is used for the <b>&ndash;computer<\/b> parameter, <b>gwmi<\/b> is an alias for the <b>Get-WmiObject<\/b> cmdlet, and <b>&ndash;ea<\/b> is the parameter alias for <b>&ndash;ErrorAction<\/b>. The enumeration value of <b>3<\/b> translates to inquire. When the cmdlet fails to connect to a remote computer named <b>win7-pc1<\/b>, it prompts for the action to take place. I respond &ldquo;h,&rdquo; which means to halt execution. Windows PowerShell then displays a message that the command was halted at user request. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; gwmi win32_bios -com win7-pc1 -ea 3<\/p>\n<p>Confirm<br \/>The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)<br \/>[Y] Yes<span>&nbsp; <\/span>[A] Yes to All<span>&nbsp; <\/span>[H] Halt Command<span>&nbsp; <\/span>[S] Suspend<span>&nbsp; <\/span>[?] Help (default is &#8220;Y&#8221;): h<br \/>Get-WmiObject : Command execution stopped because the user selected the Halt option.<br \/>At line:1 char:5<br \/>+ gwmi &lt;&lt;&lt;&lt;<span>&nbsp; <\/span>win32_bios -com win7-pc1 -ea 3<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>+ CategoryInfo<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: OperationStopped: (:) [Get-WmiObject], ParentContainsErrorRecordException<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>+ FullyQualifiedErrorId : ActionPreferenceStop,Microsoft.PowerShell.Commands.GetWmiObjectCommand<\/p>\n<p>PS C:&gt;<\/p>\n<p><br><\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\">TS, that is all there is to using the <b>&ndash;ErrorAction<\/b> preference parameter. Error Handling Week will continue tomorrow when we will talk about&hellip;wait a minute. <\/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\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/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! I enjoyed reading your blog post yesterday about using $ErrorActionPreference to control the behavior of a script. One thing I never liked about using &ldquo;On Error Resume Next&rdquo; from VBScript was that it seemed to be an all-or-nothing type of proposition. There are certain errors I do not need to [&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-51053","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! I enjoyed reading your blog post yesterday about using $ErrorActionPreference to control the behavior of a script. One thing I never liked about using &ldquo;On Error Resume Next&rdquo; from VBScript was that it seemed to be an all-or-nothing type of proposition. There are certain errors I do not need to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51053","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=51053"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51053\/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=51053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}