{"id":5116,"date":"2012-08-16T11:59:00","date_gmt":"2012-08-16T11:59:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/08\/16\/powertip-adding-a-default-exit-to-a-powershell-switch\/"},"modified":"2012-08-16T11:59:00","modified_gmt":"2012-08-16T11:59:00","slug":"powertip-adding-a-default-exit-to-a-powershell-switch","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powertip-adding-a-default-exit-to-a-powershell-switch\/","title":{"rendered":"PowerTip: Adding a Default Exit to a PowerShell Switch"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to add a default exit condition to a Windows PowerShell <strong>switch<i>&nbsp;<\/i><\/strong>statement to control execution of commands.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>I have the following switch statement, and I want to prevent the line&nbsp;<b>Write-Host<\/b>&nbsp;<b>&ldquo;switched&rdquo;<\/b>&nbsp;from executing? How can I do this?<\/p>\n<p style=\"padding-left: 60px\">$a = 3<\/p>\n<p style=\"padding-left: 60px\">switch ($a) {<\/p>\n<p style=\"padding-left: 60px\">&nbsp;1 { &#8220;one detected&#8221; }<\/p>\n<p style=\"padding-left: 60px\">&nbsp;2 { &#8220;two detected&#8221; }<\/p>\n<p style=\"padding-left: 60px\">}<\/p>\n<p style=\"padding-left: 60px\">Write-Host &#8220;switched&#8221;<\/p>\n<p style=\"padding-left: 30px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Add an exit statement to the default switch as shown here:<\/p>\n<p style=\"padding-left: 90px\">$a = 3<\/p>\n<p style=\"padding-left: 90px\">switch ($a) {<\/p>\n<p style=\"padding-left: 90px\">1 { &#8220;one detected&#8221; }<\/p>\n<p style=\"padding-left: 90px\">2 { &#8220;two detected&#8221;}<\/p>\n<p style=\"padding-left: 90px\">DEFAULT { exit}<\/p>\n<p style=\"padding-left: 90px\">}<\/p>\n<p style=\"padding-left: 90px\">Write-Host &#8220;switched&nbsp;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7610.Dr.ScriptoForTips.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7610.Dr.ScriptoForTips.jpg\" border=\"0\" alt=\"\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to add a default exit condition to a Windows PowerShell switch&nbsp;statement to control execution of commands. I have the following switch statement, and I want to prevent the line&nbsp;Write-Host&nbsp;&ldquo;switched&rdquo;&nbsp;from executing? How can I do this? $a = 3 switch ($a) { &nbsp;1 { &#8220;one detected&#8221; } &nbsp;2 { &#8220;two detected&#8221; } } [&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":[356,3,45],"class_list":["post-5116","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-powertip","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to add a default exit condition to a Windows PowerShell switch&nbsp;statement to control execution of commands. I have the following switch statement, and I want to prevent the line&nbsp;Write-Host&nbsp;&ldquo;switched&rdquo;&nbsp;from executing? How can I do this? $a = 3 switch ($a) { &nbsp;1 { &#8220;one detected&#8221; } &nbsp;2 { &#8220;two detected&#8221; } } [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5116","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=5116"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5116\/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=5116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=5116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=5116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}