{"id":78915,"date":"2016-06-22T11:59:39","date_gmt":"2016-06-22T18:59:39","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=78915"},"modified":"2019-02-18T09:10:36","modified_gmt":"2019-02-18T16:10:36","slug":"powertip-set-a-default-value-in-a-switch-statement","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powertip-set-a-default-value-in-a-switch-statement\/","title":{"rendered":"PowerTip: Set a default value in a switch statement"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use the default field in switch statement to have an assumed value.<\/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 was playing with\u00a0<strong>Select-String<\/strong> and would like to have it default to a value if none was trapped for. How can I do that?<\/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\" \/> Just use the <strong>Default<\/strong> keyword, and you can have an assumed value in the script block. In the following example, the switch is listening for the $DogSeesSquirrel object. If no values match, it returns \u2018Squirrel runs free\u2019.<\/p>\n<p style=\"padding-left: 120px\"><code>switch ($dogseesquirrel)<\/code><\/p>\n<p style=\"padding-left: 120px\"><code>{\n'Back door' { $Response=\u2019Bark!\u2019}\n'Couch'\u00a0\u00a0\u00a0\u00a0 { $Response=\u2019Growl a bit.\u2019}\n'Food bowl' { $Response=\u2019Bark! Bark! Bark!\u2019}<\/code><\/p>\n<p style=\"padding-left: 120px\">Default \u00a0\u00a0\u00a0{ $Response=\u2019Squirrel runs free\u2019 }\n}<code><\/code><\/p>\n<p style=\"padding-left: 30px\">My apologies to our little dog for this example. \ud83d\ude09<\/p>\n<p style=\"padding-left: 30px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7610.Dr.ScriptoForTips.jpg\" alt=\"The Doctor\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use the default field in switch statement to have an assumed value. I was playing with\u00a0Select-String and would like to have it default to a value if none was trapped for. How can I do that? Just use the Default keyword, and you can have an assumed value in the script block. In the [&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":[568,639,685,641],"tags":[56,356,154,45],"class_list":["post-78915","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-powertip","category-scripting-techniques","category-windows-powershell","tag-guest-blogger","tag-powertip","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use the default field in switch statement to have an assumed value. I was playing with\u00a0Select-String and would like to have it default to a value if none was trapped for. How can I do that? Just use the Default keyword, and you can have an assumed value in the script block. In the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78915","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=78915"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78915\/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=78915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=78915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=78915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}