{"id":3016,"date":"2019-10-11T08:06:24","date_gmt":"2019-10-11T15:06:24","guid":{"rendered":"https:\/\/techthoughts.info\/?p=3016"},"modified":"2019-10-11T08:06:29","modified_gmt":"2019-10-11T15:06:29","slug":"powershell-errors-and-exceptions-handling","status":"publish","type":"post","link":"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/","title":{"rendered":"PowerShell Errors and Exceptions Handling"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_69_1 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<label for=\"ez-toc-cssicon-toggle-item-69e863eed9e64\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69e863eed9e64\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#A_Tale_of_Two_Pipelines\" title=\"A Tale of Two Pipelines\">A Tale of Two Pipelines<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#Video\" title=\"Video\">Video<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#PowerShell_Error_Types\" title=\"PowerShell Error Types\">PowerShell Error Types<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#Non-terminating\" title=\"Non-terminating\">Non-terminating<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#Terminating\" title=\"Terminating\">Terminating<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#ErrorAction\" title=\"ErrorAction\">ErrorAction<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#trycatch\" title=\"try\/catch\">try\/catch<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#PowerShell_Errors_%E2%80%93_Exploring_the_error_object\" title=\"PowerShell Errors &#8211; Exploring the error object\">PowerShell Errors &#8211; Exploring the error object<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#Error_Variable\" title=\"$Error Variable\">$Error Variable<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#Closing_Example\" title=\"Closing Example\">Closing Example<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"A_Tale_of_Two_Pipelines\"><\/span>A Tale of Two Pipelines<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>PowerShell actually leverages two pipelines. If you&#8217;ve been following along in the <a rel=\"noreferrer noopener\" href=\"https:\/\/techthoughts.info\/learn-powershell-series\/\" target=\"_blank\">Learn PowerShell<\/a> series we&#8217;ve only engaged the primary pipeline so far. This is because examples provided up to this point were free from PowerShell errors. When PowerShell code runs successfully it engages the primary pipeline. This is sometimes referred to as the success pipeline or the output stream.<\/p>\n\n\n\n<p>In the real world, your PowerShell code will encounter the unexpected. When your code encounters an error or exception it can also engage the error pipeline, or the error stream. You need to be able to account for and deal with PowerShell errors, and PowerShell exceptions. In this lesson, we&#8217;ll dive into how to handle PowerShell errors and exceptions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Video\"><\/span>Video<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p> If you prefer video format over written documentation I discuss this topic in the following TechThoughts video:<\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"video-container\"><iframe loading=\"lazy\" title=\"PowerShell Errors and Exceptions Handling\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/A6afjA5Q9eM?feature=oembed&#038;wmode=opaque\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/div>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"PowerShell_Error_Types\"><\/span>PowerShell Error Types<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In many programming languages an error results in the program halting, or <em>terminating<\/em>. The program won&#8217;t continue until the issue is corrected. You have likely experienced this at some point as a program crashed, and wouldn&#8217;t resume. PowerShell doesn&#8217;t typically behave this way and doesn&#8217;t have many terminating errors.<\/p>\n\n\n\n<p>Remember that PowerShell is an operationally focused language that aims to make your life easier. If you wanted to get processes information with <strong>Get-Process<\/strong>, would you expect PowerShell to crash if it couldn&#8217;t query a process? Or would you expect it to log the error, and continue to do as much operational work as possible? Because of this operational nature, PowerShell errors and exceptions are typically <em>non-terminating<\/em>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Non-terminating\"><\/span>Non-terminating<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n# non-terminating error\n1\/0;Write-Host &#039;Hello, will I run after an error?&#039;\n<\/pre><\/div>\n\n\n<p>The above example is a classic divide by zero error. Because dividing by zero is not possible, this should throw an exception. Note the <strong>;<\/strong> symbol. In PowerShell this allows you to chain commands together. In this example that means PowerShell will first divide by zero, and then attempt to write a string to the console with <strong>Write-Host<\/strong>.<\/p>\n\n\n\n<p>Launch a new PowerShell console and try the example. PowerShell runs <em>both<\/em> commands. Notice that you still get a divide by zero error, but that doesn&#8217;t stop PowerShell from moving on to the next command.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n# non-terminating errors don&#039;t stop loops\n$collection = @(\n    &#039;C:\\Test\\newcsv.csv&#039;,\n    &#039;c:\\nope\\nope.txt&#039;\n    &#039;C:\\Test\\newcsv2.csv&#039;\n)\nforeach ($item in $collection) {\n    Get-Item $item\n}\n<\/pre><\/div>\n\n\n<p>In this example we declare an array (<strong>$collection<\/strong>) and load it with three file paths. It is very possible that one of these doesn&#8217;t actually exist, or maybe we don&#8217;t have access to it. In the foreach loop, <strong>Get-Item<\/strong> may encounter an error accessing one of these as a result. Try this example on your computer by providing files that both do and don&#8217;t exist. Notice that PowerShell will continue processing the loop, regardless of how many errors are encountered.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Terminating\"><\/span>Terminating<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>It is sometimes <em>undesirable<\/em> to have n<em>on-terminating<\/em> errors. There are several use cases where you may want to force PowerShell to terminate:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The error warrants stopping all further action<\/li><li>You want to trap and evaluate the error &#8211; possibly performing various actions based on the error information<\/li><li>You want to trap the error and hide it from the user &#8211; providing a more user friendly message<\/li><\/ul>\n\n\n\n<p>There are several ways for turning a <em>non-terminating<\/em> PowerShell error into a terminating PowerShell error.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"ErrorAction\"><\/span>ErrorAction<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p><a href=\"https:\/\/devblogs.microsoft.com\/powershell\/erroraction-and-errorvariable\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"ErrorAction (opens in a new tab)\">ErrorAction<\/a> is a common parameter that tells PowerShell what action to take when it encounters an error.  The truth is, you&#8217;re always using the ErrorAction parameter. You&#8217;re just using the default setting when you don&#8217;t specify it, which is continue. Continue will display the  error to the console and keep processing. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n# Without ErrorAction\nGet-Item -Path c:\\nope\\nope.txt;Write-Host &#039;Hello, will I run after an error?&#039;\n# With ErrorAction\nGet-Item -Path c:\\nope\\nope.txt -ErrorAction Stop;Write-Host &#039;Hello, will I run after an error?&#039;\n<\/pre><\/div>\n\n\n<p>Try the two examples above on your machine. The first <strong>Get-Item<\/strong> will error because it will be unable to find the file. <strong>Write-Host<\/strong> will continue to run. This is standard <em>non-terminating<\/em> behavior.<\/p>\n\n\n\n<p>The second example leverages ErrorAction set to Stop. This instructs PowerShell to not continue when it encounters an error. Note that <strong>Write-Host<\/strong> does not run on the example with ErrorAction. That&#8217;s because <strong>Get-Item<\/strong> <em>terminated<\/em> when it was unable to find the file.<\/p>\n\n\n\n<p>ErrorAction supports several settings:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Continue<\/strong> &#8211;  Logs error to $Error, displays error to console, continues processing. <ul><li> default (used even if ErrorAction isn&#8217;t specified) <\/li><\/ul><\/li><li><strong>Stop<\/strong> &#8211; Logs error to $Error, displays error to console, <em>terminates<\/em>.<\/li><li><strong>SilentlyContinue<\/strong> &#8211; Logs error to $Error, does not display error, continues processing.<\/li><li><strong>Ignore<\/strong> &#8211; Does not log error to $Error, does not display error, continues processing.<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"trycatch\"><\/span>try\/catch<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Placing your code into a <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.core\/about\/about_try_catch_finally?view=powershell-6\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"try catch (opens in a new tab)\">try catch<\/a> doesn&#8217;t necessarily make it <em>terminating<\/em>. PowerShell will simply <em>try<\/em> the code inside the try section, and if an error is encountered, it will move to the catch. What the catch does is up to you. The use of <strong>throw<\/strong> in the catch is one way for making an error <em>terminating<\/em>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n# throw causes PowerShell to terminate\ntry {\n    1\/0;Write-Host &#039;Hello, will I run after an error?&#039;\n}\ncatch {\n    throw\n}\n<\/pre><\/div>\n\n\n<p>Try will catch many standard errors in this manner like divide by zero, or incorrect syntax.<\/p>\n\n\n\n<p>When you have cmdlets in a try\/catch and you want PowerShell to go to the catch, you still need to specify ErrorAction. Just because a cmdlet is in a try\/catch does not mean PowerShell will go to the catch if the cmdlet encounters an error. Remember that PowerShell defaults to <strong>Continue<\/strong>. So even if an error is encountered, it will continue to process without going to the catch.<\/p>\n\n\n\n<p>Try the following:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n# this example will not go the catch and will run the Write-Host\ntry {\n    Get-Item -Path c:\\nope\\nope.txt;Write-Host &#039;Hello, will I run after an error?&#039;\n}\ncatch {\n    Write-Host &#039;You are now in the catch&#039;\n}\n\n# this example will error and go directly to the catch\ntry {\n    Get-Item -Path c:\\nope\\nope.txt -ErrorAction Stop;Write-Host &#039;Hello, will I run after an error?&#039;\n}\ncatch {\n    Write-Host &#039;You are now in the catch&#039;\n}\n<\/pre><\/div>\n\n\n<p>Now evaluate the code block below. If you run this on your machine it will try to get process information for both processes. What could you change to make this <em>terminate<\/em> the first time it encounters an error?<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n$processNames = @(\n    &#039;NotAProcess&#039;,\n    &#039;Notepad&#039;\n)\nforeach ($item in $processNames) {\n    try {\n        Get-Process -Nam $item\n    }\n    catch {\n        Write-Host $item\n        throw\n    }\n}\n<\/pre><\/div>\n\n\n<p>Keep in mind that you don&#8217;t have to <em>terminate<\/em> in the catch with <strong>throw<\/strong>. You could perform a variety of actions, such as logging the error somewhere. Using <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.utility\/write-error?view=powershell-6\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Write-Error (opens in a new tab)\">Write-Error<\/a> you can even still display the error back to the user.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n# Write-Error simply prints the error for the user\ntry {\n    1\/0;Write-Host &#039;Hello, will I run after an error?&#039;\n}\ncatch {\n    Write-Error $_\n}\n<\/pre><\/div>\n\n\n<p>You can also leverage finally with your try\/catch. Regardless if the try is performed successfully, or if the catch is run, the finally step will always be performed.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n# Finally will log results regardless of what happens\ntry {\n    Get-Content -Path c:\\nope\\nope.txt -ErrorAction Stop\n}\ncatch {\n    Write-Error $_\n}\nfinally {\n    # log results to a logging file\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"PowerShell_Errors_%E2%80%93_Exploring_the_error_object\"><\/span>PowerShell Errors &#8211; Exploring the error object<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>PowerShell deals in objects, and PowerShell errors are no exception. (A pun!) The error that is written out to the console is just a piece of the error object. When you trap the error in a try\/catch you can evaluate the error&#8217;s various properties and take action accordingly.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n#The website exists, but the page does not\ntry {\n    $webResults = Invoke-WebRequest -Uri &#039;https:\/\/techthoughts.info\/nope.htm&#039;\n}\ncatch {\n    Write-Error $_\n}\n<\/pre><\/div>\n\n\n<p>Try the example above. Because the nope.htm does not exist, <strong>Invoke-WebRequest<\/strong> will throw an exception . We will write the error to console using <strong>Write-Error $_<\/strong>. Just like for the success pipeline <strong>$_<\/strong> represents the current object in the error pipeline as well.<\/p>\n\n\n\n<p>Now, visiting a webpage that doesn&#8217;t exist technically <em>is<\/em> an exception, but not an uncommon one. Many users are familiar with miss-typing an address and getting a <em>page not found<\/em> error. If you ran the code above, it is doubtful that any of that is accurately reflected in your console. The error displayed with <strong>Write-Error<\/strong> isn&#8217;t fully articulating the issue. <\/p>\n\n\n\n<p>Depending on the nature of your code, this likely is not a good reason to terminate and stop functionality. Instead, you might advise the user to check the web address and try again. To do that, you&#8217;ll need to evaluate the error object and take the appropriate action.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n#The website exists, but the page does not\ntry {\n    $webResults = Invoke-WebRequest -Uri &#039;https:\/\/techthoughts.info\/nope.htm&#039;\n}\ncatch {\n    $theError = $_\n    if ($theError.Exception -like &quot;*404*&quot;) {\n        Write-Warning &#039;Web page not found. Check the address and try again.&#039;\n        #Retry code\n    }\n    else {\n        throw\n    }\n}\n<\/pre><\/div>\n\n\n<p>The error object has many different sub-properties. Here, we evaluate the Exception sub-property to determine if the exception message contains a 404. If it does, we can advise the user and retry. Otherwise, we&#8217;ve encountered a different type of error and it may be appropriate to <em>terminate<\/em>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n#The website does not exist\ntry {\n    $webResults = Invoke-WebRequest -Uri &#039;https:\/\/techthoughtssssssss.info\/&#039;\n}\ncatch {\n    $theError = $_\n    $theError.Exception\n}\n<\/pre><\/div>\n\n\n<p>In this example, the website doesn&#8217;t exist at all. Thus, the sub-property Exception will be different. Run this on your computer to see how it differs from the previous example.<\/p>\n\n\n\n<p>To see all the sub-properties that are contained in the error object you will need to pipe it to <strong>Format-List<\/strong> with the force parameter.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n#The website exists, but the page does not\ntry {\n    $webResults = Invoke-WebRequest -Uri &#039;https:\/\/techthoughts.info\/nope.htm&#039;\n}\ncatch {\n    $theError = $_\n    # see all the sub-properties\n    $theError | Format-List * -Force\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Error_Variable\"><\/span>$Error Variable<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>$Error is a reserved variable that contains a collection of all errors in the current session. If you have been trying the examples in this lesson your current PowerShell window should contain quite a few. Simply type <strong>$Error<\/strong> and click enter.<\/p>\n\n\n\n<p>You may be wondering what the point of this variable is. Remember that errors in PowerShell are rich data objects. As you&#8217;re developing you will likely be generating some errors as you try different solutions. If you forget to capture one, you can access the error in the <strong>$Error<\/strong> history. It&#8217;s a simple array so you can access the error and all it&#8217;s sub-properties.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n$Error&#x5B;5] | Format-List * -Force\n\n#a few other $Error commands to try\n$Error\n1\/0;$Error\nGet-Process -Name &#039;NotAProcess&#039;\n$Error\n$Error.Clear()\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Closing_Example\"><\/span>Closing Example<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n#this example will help display some helpful message to the user\n#this example will only work in PowerShell 6.1+\n$uri = Read-Host &#039;Enter the URL&#039;\ntry {\n    $webResults = Invoke-WebRequest -Uri $uri -ErrorAction Stop\n}\ncatch {\n    $statusCodeValue = $_.Exception.Response.StatusCode.value__\n    switch ($statusCodeValue) {\n        400 {\n            Write-Warning -Message &quot;HTTP Status Code 400 Bad Request. Check the URL and try again.&quot;\n        }\n        401 {\n            Write-Warning -Message &quot;HTTP Status Code 401 Unauthorized.&quot;\n        }\n        403 {\n            Write-Warning -Message &quot;HTTP Status Code 403 Forbidden. Server may be having issues. Check the URL and try again.&quot;\n        }\n        404 {\n            Write-Warning -Message &quot;HTTP Status Code 404 Not found. Check the URL and try again.&quot;\n        }\n        500 {\n            Write-Warning -Message &quot;HTTP Status Code 500 Internal Server Error.&quot;\n        }\n        Default {\n            throw\n        }\n    }\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>A Tale of Two Pipelines PowerShell actually leverages two pipelines. If you&#8217;ve been following along in the Learn PowerShell series we&#8217;ve only engaged the primary pipeline so far. This is because examples provided up&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":3046,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[421],"tags":[265],"series":[436],"class_list":["post-3016","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-powershell","series-learn-powershell"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PowerShell Errors and Exceptions Handling - Tech Thoughts<\/title>\n<meta name=\"description\" content=\"Learn how to handle PowerShell Errors and Exceptions. Explore PowerShell&#039;s non-terminating vs terminating behavior. Explore rich PowerShell error objects.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PowerShell Errors and Exceptions Handling - Tech Thoughts\" \/>\n<meta property=\"og:description\" content=\"Learn how to handle PowerShell Errors and Exceptions. Explore PowerShell&#039;s non-terminating vs terminating behavior. Explore rich PowerShell error objects.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/\" \/>\n<meta property=\"og:site_name\" content=\"Tech Thoughts\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/techthoughts2\" \/>\n<meta property=\"article:published_time\" content=\"2019-10-11T15:06:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-10-11T15:06:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/10\/powershell_errors.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jake\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JakeMorrison\" \/>\n<meta name=\"twitter:site\" content=\"@JakeMorrison\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jake\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/\"},\"author\":{\"name\":\"Jake\",\"@id\":\"https:\/\/www.techthoughts.info\/#\/schema\/person\/422a290e8bc3f6a94775f6290c26105d\"},\"headline\":\"PowerShell Errors and Exceptions Handling\",\"datePublished\":\"2019-10-11T15:06:24+00:00\",\"dateModified\":\"2019-10-11T15:06:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/\"},\"wordCount\":1411,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/www.techthoughts.info\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/10\/powershell_errors.jpg\",\"keywords\":[\"PowerShell\"],\"articleSection\":[\"Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/\",\"url\":\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/\",\"name\":\"PowerShell Errors and Exceptions Handling - Tech Thoughts\",\"isPartOf\":{\"@id\":\"https:\/\/www.techthoughts.info\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/10\/powershell_errors.jpg\",\"datePublished\":\"2019-10-11T15:06:24+00:00\",\"dateModified\":\"2019-10-11T15:06:29+00:00\",\"description\":\"Learn how to handle PowerShell Errors and Exceptions. Explore PowerShell's non-terminating vs terminating behavior. Explore rich PowerShell error objects.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#primaryimage\",\"url\":\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/10\/powershell_errors.jpg\",\"contentUrl\":\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/10\/powershell_errors.jpg\",\"width\":1280,\"height\":720,\"caption\":\"Learn PowerShell Episode 8 - PowerShell Errors and Exceptions Handling\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Tech Thoughts\",\"item\":\"https:\/\/www.techthoughts.info\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PowerShell Errors and Exceptions Handling\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.techthoughts.info\/#website\",\"url\":\"https:\/\/www.techthoughts.info\/\",\"name\":\"Tech Thoughts\",\"description\":\"A cloud, PowerShell, server, hardware, technology, and more tech thoughts blog\",\"publisher\":{\"@id\":\"https:\/\/www.techthoughts.info\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.techthoughts.info\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.techthoughts.info\/#organization\",\"name\":\"TechThoughts\",\"url\":\"https:\/\/www.techthoughts.info\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.techthoughts.info\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2017\/12\/techthoughts_icon.png\",\"contentUrl\":\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2017\/12\/techthoughts_icon.png\",\"width\":110,\"height\":109,\"caption\":\"TechThoughts\"},\"image\":{\"@id\":\"https:\/\/www.techthoughts.info\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/techthoughts2\",\"https:\/\/x.com\/JakeMorrison\",\"https:\/\/www.youtube.com\/TechthoughtsInfo2\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.techthoughts.info\/#\/schema\/person\/422a290e8bc3f6a94775f6290c26105d\",\"name\":\"Jake\",\"description\":\"A little about me...\",\"sameAs\":[\"https:\/\/jakewmorrison.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PowerShell Errors and Exceptions Handling - Tech Thoughts","description":"Learn how to handle PowerShell Errors and Exceptions. Explore PowerShell's non-terminating vs terminating behavior. Explore rich PowerShell error objects.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/","og_locale":"en_US","og_type":"article","og_title":"PowerShell Errors and Exceptions Handling - Tech Thoughts","og_description":"Learn how to handle PowerShell Errors and Exceptions. Explore PowerShell's non-terminating vs terminating behavior. Explore rich PowerShell error objects.","og_url":"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/","og_site_name":"Tech Thoughts","article_publisher":"https:\/\/www.facebook.com\/techthoughts2","article_published_time":"2019-10-11T15:06:24+00:00","article_modified_time":"2019-10-11T15:06:29+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/10\/powershell_errors.jpg","type":"image\/jpeg"}],"author":"Jake","twitter_card":"summary_large_image","twitter_creator":"@JakeMorrison","twitter_site":"@JakeMorrison","twitter_misc":{"Written by":"Jake","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#article","isPartOf":{"@id":"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/"},"author":{"name":"Jake","@id":"https:\/\/www.techthoughts.info\/#\/schema\/person\/422a290e8bc3f6a94775f6290c26105d"},"headline":"PowerShell Errors and Exceptions Handling","datePublished":"2019-10-11T15:06:24+00:00","dateModified":"2019-10-11T15:06:29+00:00","mainEntityOfPage":{"@id":"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/"},"wordCount":1411,"commentCount":2,"publisher":{"@id":"https:\/\/www.techthoughts.info\/#organization"},"image":{"@id":"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#primaryimage"},"thumbnailUrl":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/10\/powershell_errors.jpg","keywords":["PowerShell"],"articleSection":["Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/","url":"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/","name":"PowerShell Errors and Exceptions Handling - Tech Thoughts","isPartOf":{"@id":"https:\/\/www.techthoughts.info\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#primaryimage"},"image":{"@id":"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#primaryimage"},"thumbnailUrl":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/10\/powershell_errors.jpg","datePublished":"2019-10-11T15:06:24+00:00","dateModified":"2019-10-11T15:06:29+00:00","description":"Learn how to handle PowerShell Errors and Exceptions. Explore PowerShell's non-terminating vs terminating behavior. Explore rich PowerShell error objects.","breadcrumb":{"@id":"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#primaryimage","url":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/10\/powershell_errors.jpg","contentUrl":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/10\/powershell_errors.jpg","width":1280,"height":720,"caption":"Learn PowerShell Episode 8 - PowerShell Errors and Exceptions Handling"},{"@type":"BreadcrumbList","@id":"https:\/\/www.techthoughts.info\/powershell-errors-and-exceptions-handling\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Tech Thoughts","item":"https:\/\/www.techthoughts.info\/"},{"@type":"ListItem","position":2,"name":"PowerShell Errors and Exceptions Handling"}]},{"@type":"WebSite","@id":"https:\/\/www.techthoughts.info\/#website","url":"https:\/\/www.techthoughts.info\/","name":"Tech Thoughts","description":"A cloud, PowerShell, server, hardware, technology, and more tech thoughts blog","publisher":{"@id":"https:\/\/www.techthoughts.info\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.techthoughts.info\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.techthoughts.info\/#organization","name":"TechThoughts","url":"https:\/\/www.techthoughts.info\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.techthoughts.info\/#\/schema\/logo\/image\/","url":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2017\/12\/techthoughts_icon.png","contentUrl":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2017\/12\/techthoughts_icon.png","width":110,"height":109,"caption":"TechThoughts"},"image":{"@id":"https:\/\/www.techthoughts.info\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/techthoughts2","https:\/\/x.com\/JakeMorrison","https:\/\/www.youtube.com\/TechthoughtsInfo2"]},{"@type":"Person","@id":"https:\/\/www.techthoughts.info\/#\/schema\/person\/422a290e8bc3f6a94775f6290c26105d","name":"Jake","description":"A little about me...","sameAs":["https:\/\/jakewmorrison.com"]}]}},"jetpack_featured_media_url":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/10\/powershell_errors.jpg","jetpack_shortlink":"https:\/\/wp.me\/p2EFB0-ME","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/posts\/3016"}],"collection":[{"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/comments?post=3016"}],"version-history":[{"count":15,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/posts\/3016\/revisions"}],"predecessor-version":[{"id":3047,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/posts\/3016\/revisions\/3047"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/media\/3046"}],"wp:attachment":[{"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/media?parent=3016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/categories?post=3016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/tags?post=3016"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/series?post=3016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}