{"id":1035,"date":"2014-07-09T00:01:00","date_gmt":"2014-07-09T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/07\/09\/handling-errors-the-powershell-way\/"},"modified":"2014-07-09T00:01:00","modified_gmt":"2014-07-09T00:01:00","slug":"handling-errors-the-powershell-way","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/handling-errors-the-powershell-way\/","title":{"rendered":"Handling Errors the PowerShell Way"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Trevor Sullivan talks about handling errors in Windows PowerShell.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today we have guest blogger and Windows PowerShell MVP, <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/tag\/trevor-sullivan\/\">Trevor Sullivan<\/a>&#8230; also find Trevor on <span>Twitter (<a href=\"https:\/\/twitter.com\/pcgeek86\"><span style=\"color:#0000ff\">https:\/\/twitter.com\/pcgeek86<\/span><\/a>) and his blog (<a href=\"http:\/\/trevorsullivan.net\/\"><span style=\"color:#0000ff\">http:\/\/trevorsullivan.net<\/span><\/a>) <\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, just wrote a post about how to use the<strong> Try-Catch-Finally<\/strong> blocks in Windows PowerShell. But have you ever wondered if that was the only way to handle errors? It turns out that although it&rsquo;s a great way to handle errors, there are still other options!<\/p>\n<p>If you&rsquo;re coming to Windows PowerShell from a software development background, you&rsquo;ll most likely pick up on <strong>Try-Catch-Finally<\/strong> pretty easily. On the other hand, if you&rsquo;re new to scripting, or you are a curious, knowledge-driven individual, you might want to consider what we&rsquo;re talking about today.<\/p>\n<h2>Common parameters<\/h2>\n<p>When Windows PowerShell&nbsp;2.0 came out, a new concept was introduced, called Advanced Functions. This concept allows you to develop commands that have the same feel as compiled cmdlets, while writing them in Windows PowerShell script syntax. One of the benefits of developing cmdlet-style commands instead of basic functions, is that they offer a few &ldquo;common parameters.&rdquo; Two of these common parameters are related to error handling: <b>-ErrorAction<\/b> and <b>-ErrorVariable<\/b>.<\/p>\n<p>For more information about common parameters in advanced functions and compiled cmdlets, run this command at the Windows PowerShell prompt:<\/p>\n<p style=\"margin-left:30px\">Get-Help -Name about_CommonParameters;<\/p>\n<p style=\"margin-left:30px\">ErrorVariable Parameter<\/p>\n<p>Normally, if you run a Windows PowerShell command and an error occurs, the error record will be appended to the &ldquo;automatic variable&rdquo; named <b>$error<\/b>. When you use the <b>-ErrorVariable<\/b> parameter in a call to a command, the error is assigned to the variable name that you specify. It&rsquo;s important to note that even when you use the <b>-ErrorVariable<\/b> parameter, the <b>$error<\/b> variable is still updated.<\/p>\n<p>By default, the <b>-ErrorVariable<\/b> parameter will overwrite the variable with the name that you specify. If you want to append an error to the variable, instead of overwriting it, you can put a plus sign (<b>+<\/b>) in front of the variable name. Let&rsquo;s take a look at an example:<\/p>\n<p style=\"margin-left:30px\">Stop-Process -Name invalidprocess -ErrorVariable ProcessError;<br \/> $ProcessError;<br \/> Stop-Process -Name invalidprocess2 -ErrorVariable +ProcessError;<br \/> if ($ProcessError) {<br \/> &nbsp;&nbsp;&nbsp; ######## Take administrative action on error state<br \/> }<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-9-14-1.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-9-14-1.png\" \/><\/a><\/p>\n<h2>ErrorAction parameter<\/h2>\n<p>The <b>-ErrorAction<\/b> common parameter allows you to specify which action to take if a command fails. The available options are: <b>Stop<\/b>, <b>Continue<\/b>, <b>SilentlyContinue<\/b>, <b>Ignore<\/b>, or <b>Inquire<\/b>. If you&rsquo;re developing a Windows PowerShell workflow, you can also use the <b>Suspend<\/b> value. However, advanced functions cannot be suspended.<\/p>\n<p>When you specify the <b>ErrorAction<\/b> parameter during a call to a command, the specified behavior will override the <b>$ErrorActionPreference<\/b> variable in Windows PowerShell. This variable is part of a handful of variables known as &ldquo;preference variables.&rdquo; By default, Windows PowerShell uses an error action preference of <b>Continue<\/b>, which means that errors will be written out to the host, but the script will continue to execute. Hence, these types of errors are known as &ldquo;non-terminating&rdquo; errors.<\/p>\n<p>If you set <b>$ErrorActionPreference<\/b> to <b>Stop<\/b> or if you use <b>Stop<\/b> as the parameter value for <b>-ErrorAction<\/b>, Windows PowerShell will stop the script execution at the point an error occurs. When these errors occur, they are considered &ldquo;terminating errors.&rdquo;<\/p>\n<p>As an example, if you want to stop the execution of your Windows PowerShell script when an error occurs during a call to <b>Stop-Process<\/b>, you can simply add the <b>-ErrorAction<\/b> parameter and use the value <b>Stop<\/b>:<\/p>\n<p style=\"margin-left:30px\">Stop-Process -Name invalidprocess -ErrorAction Stop;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-9-14-2.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-9-14-2.png\" \/><\/a><\/p>\n<p>If you want to suppress the errors from being displayed, you can use the value <b>SilentlyContinue<\/b>, for example:<\/p>\n<p style=\"margin-left:30px\">Stop-Process -Name invalidprocess -ErrorAction SilentlyContinue;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-9-14-3.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-9-14-3.png\" \/><\/a><\/p>\n<p>You can also combine the <b>ErrorAction<\/b> and <b>ErrorVariable<\/b> parameters, such as in this example:<\/p>\n<p style=\"margin-left:30px\">Stop-Process &ndash;Name invalidprocess -ErrorAction SilentlyContinue -ErrorVariable ProcessError;<\/p>\n<p style=\"margin-left:30px\">If ($ProcessError) {<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; ####### Something went wrong<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-9-14-4.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-9-14-4.png\" \/><\/a><\/p>\n<p>We have explored the use of two Windows PowerShell common parameters, <b>ErrorAction<\/b> and <b>ErrorVariable<\/b>. I hope that this post has enlightened you about the use of these variables and how to use them to direct the execution flow of your scripts. Thank you for reading, and I will see you next time!<\/p>\n<p>~Trevor<\/p>\n<p>Thank you, Trevor, for taking the time to write this explanation and sharing it with our readers. You can reach Trevor on Twitter (<a href=\"https:\/\/twitter.com\/pcgeek86\" target=\"_blank\">https:\/\/twitter.com\/pcgeek86<\/a>) or&nbsp;follow him on his blog, <a href=\"http:\/\/trevorsullivan.net\/\" target=\"_blank\">Trevor Sullivan&#039;s Tech Room, Minding the gap between administration and development<\/a>.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Trevor Sullivan talks about handling errors in Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Today we have guest blogger and Windows PowerShell MVP, Trevor Sullivan&#8230; also find Trevor on Twitter (https:\/\/twitter.com\/pcgeek86) and his blog (http:\/\/trevorsullivan.net) Microsoft Scripting Guy, Ed Wilson, just wrote a post about how to use the Try-Catch-Finally blocks in [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,56,3,4,211,45],"class_list":["post-1035","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-guest-blogger","tag-scripting-guy","tag-scripting-techniques","tag-trevor-sullivan","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Trevor Sullivan talks about handling errors in Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Today we have guest blogger and Windows PowerShell MVP, Trevor Sullivan&#8230; also find Trevor on Twitter (https:\/\/twitter.com\/pcgeek86) and his blog (http:\/\/trevorsullivan.net) Microsoft Scripting Guy, Ed Wilson, just wrote a post about how to use the Try-Catch-Finally blocks in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1035","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=1035"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1035\/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=1035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}