{"id":567,"date":"2018-07-23T08:00:06","date_gmt":"2018-07-23T14:00:06","guid":{"rendered":"https:\/\/practicalpowershell.com\/?p=567"},"modified":"2020-03-14T16:25:03","modified_gmt":"2020-03-14T22:25:03","slug":"basic-error-handling","status":"publish","type":"post","link":"https:\/\/practicalpowershell.com\/basic-error-handling\/","title":{"rendered":"Basic Error Handling"},"content":{"rendered":"<p><a href=\"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Error.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Error.jpg?resize=150%2C150&#038;ssl=1\" alt=\"\" width=\"150\" height=\"150\" class=\"alignleft size-thumbnail wp-image-568\" srcset=\"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Error.jpg?resize=150%2C150&amp;ssl=1 150w, https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Error.jpg?resize=100%2C100&amp;ssl=1 100w, https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Error.jpg?zoom=2&amp;resize=150%2C150&amp;ssl=1 300w, https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Error.jpg?zoom=3&amp;resize=150%2C150&amp;ssl=1 450w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a>Like all programming languages, PowerShell has a form of error handling to help the programmer or scripter, as the case may be. Error handling is meant to help troubleshoot a script or at the very least reveal a problem has occurred and possibly provide an error message. There are many ways to handle errors in PowerShell scripts. We can suppress the error messages, trigger another action with an error message, skip parts of scripts, and more. <BR><br \/>\nOne type of error handling is the use of the Try {} Catch {} pair. It can be used to trigger actions if a particular code set fails. The error action can be tailored to the specific needs of the script. <BR><br \/>\n<u>Example (1) &#8211; Sending Email through Office 365<\/u><BR><br \/>\n<em>Sample code no error handling<\/em><br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\n$To = &#039;recipient@domain.com&#039;<br \/>\n$From = &#039;sender@domain.com&#039;<br \/>\n$Subject = &#039;Test Email&#039;<br \/>\n$SMTPServer = &#039;smtp.office365.com&#039;<br \/>\n$Port = &#039;587&#039;<br \/>\nSend-MailMessage -To $To -From $From -Subject $Subject -SMTPServer $SMTPServer -Credential $SMTPCred -Port $Port -UseSsl -ErrorAction STOP<br \/>\n[\/sourcecode]<br \/>\nIn the above code, if the Send-MailMessage command fails we&#8217;ll simply get a bunch of red errors from PowerShell. No option for recourse or notification. In order to handle this type of situation, we use Try {} Catch {} like so:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nTry {<br \/>\nSend-MailMessage -To $To -From $From -Subject $Subject -SMTPServer $SMTPServer -Credential $SMTPCred -Port $Port -UseSsl -ErrorAction STOP<br \/>\n} Catch [Exception] {<br \/>\nWrite-host &quot; &quot;<br \/>\nWrite-host &quot;Sending email from $From to $To failed &#8211; Try again.&quot; -ForegroundColor Red<br \/>\nWrite-host &quot; &quot;<br \/>\nWrite-host &quot; &#8212; Error Message &#8212; &quot; -ForegroundColor Yellow<br \/>\n$Exception = $_.Exception.GetType().FullName<br \/>\n$Message = $_.Exception.Message<br \/>\nWrite-host &quot; $Exception&quot; -ForegroundColor Yellow<br \/>\nWrite-host &quot; $Message&quot; -ForegroundColor Yellow<br \/>\n}<br \/>\n[\/sourcecode]<br \/>\nA sample error message with the new code:<br \/>\n<a href=\"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Error-Try-Catch.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Error-Try-Catch.png?resize=640%2C78&#038;ssl=1\" alt=\"\" width=\"640\" height=\"78\" class=\"aligncenter size-full wp-image-569\" srcset=\"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Error-Try-Catch.png?w=804&amp;ssl=1 804w, https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Error-Try-Catch.png?resize=300%2C37&amp;ssl=1 300w, https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Error-Try-Catch.png?resize=768%2C94&amp;ssl=1 768w, https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Error-Try-Catch.png?resize=600%2C73&amp;ssl=1 600w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><br \/>\nBeyond just displaying simple messages, Try and Catch can be used for other actions:<\/p>\n<ul>\n<li>Event Log Message\n<li>Modifying a Log File\n<li>Changing a variable from True to False or some other relevant value.\n<\/ul>\n<p>The purpose could be to use the change to trigger an event further along in the script (variable) or simply keep track of failures for auditing purposes. Again depending on the purpose.<BR><br \/>\n<u>Further Reading<\/u><br \/>\n<a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.core\/about\/about_try_catch_finally?view=powershell-6\">https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.core\/about\/about_try_catch_finally?view=powershell-6 <\/a><br \/>\n<a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/07\/05\/weekend-scripter-using-try-catch-finally-blocks-for-powershell-error-handling\/\">https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/07\/05\/weekend-scripter-using-try-catch-finally-blocks-for-powershell-error-handling\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Like all programming languages, PowerShell has a form of error handling to help the programmer or scripter, as the case may be. Error handling is meant to help troubleshoot a script or at the very least reveal a problem has occurred and possibly provide an error message. There are many ways to handle errors in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rop_custom_images_group":[],"rop_custom_messages_group":[],"rop_publish_now":"initial","rop_publish_now_accounts":[],"rop_publish_now_history":[],"rop_publish_now_status":"pending","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-567","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Basic Error Handling - Practical PowerShell<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/practicalpowershell.com\/basic-error-handling\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Basic Error Handling - Practical PowerShell\" \/>\n<meta property=\"og:description\" content=\"Like all programming languages, PowerShell has a form of error handling to help the programmer or scripter, as the case may be. Error handling is meant to help troubleshoot a script or at the very least reveal a problem has occurred and possibly provide an error message. There are many ways to handle errors in [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/practicalpowershell.com\/basic-error-handling\/\" \/>\n<meta property=\"og:site_name\" content=\"Practical PowerShell\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-23T14:00:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-03-14T22:25:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Error-150x150.jpg\" \/>\n<meta name=\"author\" content=\"damian\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"damian\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/basic-error-handling\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/basic-error-handling\\\/\"},\"author\":{\"name\":\"damian\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"headline\":\"Basic Error Handling\",\"datePublished\":\"2018-07-23T14:00:06+00:00\",\"dateModified\":\"2020-03-14T22:25:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/basic-error-handling\\\/\"},\"wordCount\":376,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"image\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/basic-error-handling\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/practicalpowershell.com\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/Error-150x150.jpg\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/practicalpowershell.com\\\/basic-error-handling\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/basic-error-handling\\\/\",\"url\":\"https:\\\/\\\/practicalpowershell.com\\\/basic-error-handling\\\/\",\"name\":\"Basic Error Handling - Practical PowerShell\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/basic-error-handling\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/basic-error-handling\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/practicalpowershell.com\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/Error-150x150.jpg\",\"datePublished\":\"2018-07-23T14:00:06+00:00\",\"dateModified\":\"2020-03-14T22:25:03+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/basic-error-handling\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/practicalpowershell.com\\\/basic-error-handling\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/basic-error-handling\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/practicalpowershell.com\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/Error.jpg?fit=450%2C300&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/practicalpowershell.com\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/Error.jpg?fit=450%2C300&ssl=1\",\"width\":450,\"height\":300},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/basic-error-handling\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/practicalpowershell.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Basic Error Handling\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#website\",\"url\":\"https:\\\/\\\/practicalpowershell.com\\\/\",\"name\":\"Practical PowerShell\",\"description\":\"PowerShell books written by experts\",\"publisher\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/practicalpowershell.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\",\"name\":\"damian\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\",\"caption\":\"damian\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Basic Error Handling - Practical PowerShell","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:\/\/practicalpowershell.com\/basic-error-handling\/","og_locale":"en_US","og_type":"article","og_title":"Basic Error Handling - Practical PowerShell","og_description":"Like all programming languages, PowerShell has a form of error handling to help the programmer or scripter, as the case may be. Error handling is meant to help troubleshoot a script or at the very least reveal a problem has occurred and possibly provide an error message. There are many ways to handle errors in [&hellip;]","og_url":"https:\/\/practicalpowershell.com\/basic-error-handling\/","og_site_name":"Practical PowerShell","article_published_time":"2018-07-23T14:00:06+00:00","article_modified_time":"2020-03-14T22:25:03+00:00","og_image":[{"url":"https:\/\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Error-150x150.jpg","type":"","width":"","height":""}],"author":"damian","twitter_card":"summary_large_image","twitter_misc":{"Written by":"damian","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/practicalpowershell.com\/basic-error-handling\/#article","isPartOf":{"@id":"https:\/\/practicalpowershell.com\/basic-error-handling\/"},"author":{"name":"damian","@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"headline":"Basic Error Handling","datePublished":"2018-07-23T14:00:06+00:00","dateModified":"2020-03-14T22:25:03+00:00","mainEntityOfPage":{"@id":"https:\/\/practicalpowershell.com\/basic-error-handling\/"},"wordCount":376,"commentCount":0,"publisher":{"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"image":{"@id":"https:\/\/practicalpowershell.com\/basic-error-handling\/#primaryimage"},"thumbnailUrl":"https:\/\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Error-150x150.jpg","inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/practicalpowershell.com\/basic-error-handling\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/practicalpowershell.com\/basic-error-handling\/","url":"https:\/\/practicalpowershell.com\/basic-error-handling\/","name":"Basic Error Handling - Practical PowerShell","isPartOf":{"@id":"https:\/\/practicalpowershell.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/practicalpowershell.com\/basic-error-handling\/#primaryimage"},"image":{"@id":"https:\/\/practicalpowershell.com\/basic-error-handling\/#primaryimage"},"thumbnailUrl":"https:\/\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Error-150x150.jpg","datePublished":"2018-07-23T14:00:06+00:00","dateModified":"2020-03-14T22:25:03+00:00","breadcrumb":{"@id":"https:\/\/practicalpowershell.com\/basic-error-handling\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/practicalpowershell.com\/basic-error-handling\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/practicalpowershell.com\/basic-error-handling\/#primaryimage","url":"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Error.jpg?fit=450%2C300&ssl=1","contentUrl":"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Error.jpg?fit=450%2C300&ssl=1","width":450,"height":300},{"@type":"BreadcrumbList","@id":"https:\/\/practicalpowershell.com\/basic-error-handling\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/practicalpowershell.com\/"},{"@type":"ListItem","position":2,"name":"Basic Error Handling"}]},{"@type":"WebSite","@id":"https:\/\/practicalpowershell.com\/#website","url":"https:\/\/practicalpowershell.com\/","name":"Practical PowerShell","description":"PowerShell books written by experts","publisher":{"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/practicalpowershell.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb","name":"damian","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g","caption":"damian"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g"}}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/comments?post=567"}],"version-history":[{"count":2,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/567\/revisions"}],"predecessor-version":[{"id":571,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/567\/revisions\/571"}],"wp:attachment":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/media?parent=567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/categories?post=567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/tags?post=567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}