{"id":4379,"date":"2016-04-15T23:23:19","date_gmt":"2016-04-16T06:23:19","guid":{"rendered":"http:\/\/tommymaynard.com\/?p=4379"},"modified":"2022-12-25T19:09:08","modified_gmt":"2022-12-26T02:09:08","slug":"powershell-nested-switch-statements-2016","status":"publish","type":"post","link":"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/","title":{"rendered":"PowerShell Nested Switch Statements"},"content":{"rendered":"<p>There was a Reddit post recently that gave me an opportunity to try something I&#8217;m not sure I&#8217;ve tried before: nested switch statements\u2014-switch statements inside switch statements. So we&#8217;re on the same page, I&#8217;ve written an example of a basic, non-nested switch. It provides some of the same functionality as an If-ElseIf. I&#8217;ll typically use a switch statement instead of an If-ElseIf when there&#8217;s three or more outcomes. It&#8217;s easier to read, and it looks cleaner.<\/p>\n<p>In the example below, a specific phrase will be written if the value in the $Fruit variable is apple, banana, or pear. If it&#8217;s not one of those, it&#8217;ll take the default action. In the case below, the default action indicates that an apple, banana, or pear wasn&#8217;t chosen. Switch statements\u00a0will sometimes be called case statements in other languages.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$Fruit = 'banana'\r\nSwitch ($Fruit) {\r\n    'apple' {'You chose apple.'; break}\r\n    'banana' {'You chose banana.'; break}\r\n    'pear' {'You chose pear.'; break}\r\n    default {'You didn''t choose an apple, banana, or pear.'}\r\n}\r\n<\/pre>\n<p>As a part of my nested switch statement testing, I wrote this example using automobile makes and models. The outermost switch statement makes a determination of which nested switch statement to execute.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$Make = 'Ford'\r\n$Model = 'Escape'\r\n \r\nSwitch ($Make) {\r\n    'Ford' {\r\n        Switch ($Model) {\r\n            'Mustang' {Write-Output -InputObject 'You selected the Ford Mustang.'; break}\r\n            'Escape' {Write-Output -InputObject 'You selected the Ford Escape.'; break}\r\n            default {Write-Output -InputObject &quot;You selected Ford, however, the $Model isn't a valid model for this make.&quot;; break}\r\n        }\r\n    }\r\n    'Chevy' {\r\n        Switch ($Model) {\r\n            'Corvette' {Write-Output -InputObject 'You selected the Chevy Corvette.'; break}\r\n            'Camaro' {Write-Output -InputObject 'You selected the Chevy Camaro.'; break}\r\n            default {Write-Output -InputObject &quot;You selected Chevy, however, the $Model isn't a valid model for this make.&quot;; break}\r\n        }\r\n    }\r\n    'Dodge' {\r\n        Switch ($Model) {\r\n            'Charger' {Write-Output -InputObject 'You selected the Dodge Charger.'; break}\r\n            'Viper' {Write-Output -InputObject 'You selected the Dodge Viper.'; break}\r\n            default {Write-Output -InputObject &quot;You selected Dodge, however, the $Model isn't a valid model for this make.&quot;; break}\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Should we take this further\u2014-a switch statement inside a switch statement inside a switch statement? I&#8217;m not going to lie, it started to get confusing and so my first recommendation would be to try and steer clear of the multi-nesting this deep (3 levels). I&#8217;ve removed some of the example above\u2014-Chevy and Dodge\u2014-in my example below and focused on the Ford (pun, absolutely intended). To help, I&#8217;ve included comments on the closing brackets. This multi-nested switch statement will return different values based on the make, the model, and the year, too.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$Make = 'Ford'\r\n$Model = 'Escape'\r\n$Year = '2016'\r\n  \r\nSwitch ($Make) {\r\n    'Ford' {\r\n        Switch ($Model) {\r\n            'Mustang' {\r\n                Switch ($Year) {\r\n                    '2015' {Write-Output -InputObject &quot;You selected the 2015 Ford Mustang.&quot;; break}\r\n                    '2016' {Write-Output -InputObject &quot;You selected the 2016 Ford Mustang&quot;; break}\r\n                    default {Write-Output -InputObject &quot;You selected the Ford Mustang with a non-matching year.&quot;; break}\r\n                } # End Mustang Year Switch.\r\n            } # End Mustang Switch.\r\n            'Escape' {\r\n                Switch ($Year) {\r\n                    '2015' {Write-Output -InputObject &quot;You selected the 2015 Ford Escape.&quot;; break}\r\n                    '2016' {Write-Output -InputObject &quot;You selected the 2016 Ford Escape.&quot;; break}\r\n                    default {Write-Output -InputObject &quot;You selected the Ford Escape with a non-matching year.&quot;; break}\r\n                } # End Escape Year Switch.\r\n            } # End Escape Switch.\r\n            default {Write-Output -InputObject &quot;You selected a Ford; however, the $Model isn't a valid model for this make.&quot;}\r\n        } # End Ford Model Switch.\r\n    } # End Ford Switch.\r\n    default {Write-Output -InputObject 'Sorry, we''re only dealing with Fords (at the moment).'}\r\n} # End Make Switch.\r\n<\/pre>\n<p>So I can track it down later, here&#8217;s the post I read on <a href=\"https:\/\/www.reddit.com\/r\/PowerShell\/comments\/4exrgd\/have_a_bunch_of_functions_working_locally_having\" target=\"_blank\" rel=\"noopener\">Reddit<\/a> that influenced this post. It includes the link to my possible, partial solution; however, that direct link is here: <a href=\"http:\/\/pastebin.com\/KRxRb1VM\" target=\"_blank\" rel=\"noopener\">http:\/\/pastebin.com\/KRxRb1VM<\/a>.<\/p>\n<p><strong>Update<\/strong>: It has occurred to me that it might be easier to follow this thrice nested switch without all the comments. I&#8217;ve removed those below.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$Make = 'Ford'\r\n$Model = 'Escape'\r\n$Year = '2016'\r\n  \r\nSwitch ($Make) {\r\n    'Ford' {\r\n        Switch ($Model) {\r\n            'Mustang' {\r\n                Switch ($Year) {\r\n                    '2015' {Write-Output -InputObject &quot;You selected the 2015 Ford Mustang.&quot;; break}\r\n                    '2016' {Write-Output -InputObject &quot;You selected the 2016 Ford Mustang&quot;; break}\r\n                    default {Write-Output -InputObject &quot;You selected the Ford Mustang with a non-matching year.&quot;; break}\r\n                }\r\n            }\r\n            'Escape' {\r\n                Switch ($Year) {\r\n                    '2015' {Write-Output -InputObject &quot;You selected the 2015 Ford Escape.&quot;; break}\r\n                    '2016' {Write-Output -InputObject &quot;You selected the 2016 Ford Escape.&quot;; break}\r\n                    default {Write-Output -InputObject &quot;You selected the Ford Escape with a non-matching year.&quot;; break}\r\n                }\r\n            }\r\n            default {Write-Output -InputObject &quot;You selected a Ford; however, the $Model isn't a valid model for this make.&quot;}\r\n        }\r\n    }\r\n    default {Write-Output -InputObject 'Sorry, we''re only dealing with Fords (at the moment).'}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There was a Reddit post recently that gave me an opportunity to try something I&#8217;m not sure I&#8217;ve tried before: nested switch statements\u2014-switch statements inside switch statements. So we&#8217;re on the same page, I&#8217;ve written an example of a basic, non-nested switch. It provides some of the same functionality as an If-ElseIf. I&#8217;ll typically use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","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":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[2],"tags":[379,378,147],"class_list":["post-4379","post","type-post","status-publish","format-standard","hentry","category-quick-learn","tag-if-elseif","tag-switch","tag-write-output"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PowerShell Nested Switch Statements - tommymaynard.com<\/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:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PowerShell Nested Switch Statements - tommymaynard.com\" \/>\n<meta property=\"og:description\" content=\"There was a Reddit post recently that gave me an opportunity to try something I&#8217;m not sure I&#8217;ve tried before: nested switch statements\u2014-switch statements inside switch statements. So we&#8217;re on the same page, I&#8217;ve written an example of a basic, non-nested switch. It provides some of the same functionality as an If-ElseIf. I&#8217;ll typically use [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/\" \/>\n<meta property=\"og:site_name\" content=\"tommymaynard.com\" \/>\n<meta property=\"article:published_time\" content=\"2016-04-16T06:23:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-12-26T02:09:08+00:00\" \/>\n<meta name=\"author\" content=\"Tommy Maynard\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Tommy Maynard\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/\"},\"author\":{\"name\":\"Tommy Maynard\",\"@id\":\"https:\/\/tommymaynard.com\/#\/schema\/person\/c74505a44d3d93dc7f82a08502d81ff8\"},\"headline\":\"PowerShell Nested Switch Statements\",\"datePublished\":\"2016-04-16T06:23:19+00:00\",\"dateModified\":\"2022-12-26T02:09:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/\"},\"wordCount\":772,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/tommymaynard.com\/#\/schema\/person\/c74505a44d3d93dc7f82a08502d81ff8\"},\"keywords\":[\"If-ElseIf\",\"Switch\",\"Write-Output\"],\"articleSection\":[\"Quick Learn\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/\",\"url\":\"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/\",\"name\":\"PowerShell Nested Switch Statements - tommymaynard.com\",\"isPartOf\":{\"@id\":\"https:\/\/tommymaynard.com\/#website\"},\"datePublished\":\"2016-04-16T06:23:19+00:00\",\"dateModified\":\"2022-12-26T02:09:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tommymaynard.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PowerShell Nested Switch Statements\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tommymaynard.com\/#website\",\"url\":\"https:\/\/tommymaynard.com\/\",\"name\":\"tommymaynard.com\",\"description\":\"A PowerShell Resource. Since June 2014.\",\"publisher\":{\"@id\":\"https:\/\/tommymaynard.com\/#\/schema\/person\/c74505a44d3d93dc7f82a08502d81ff8\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tommymaynard.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/tommymaynard.com\/#\/schema\/person\/c74505a44d3d93dc7f82a08502d81ff8\",\"name\":\"Tommy Maynard\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/ff5e885ce365c2b895df63b9b0d98a7f1a1d1421def6bfb59336a95460fc2329?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ff5e885ce365c2b895df63b9b0d98a7f1a1d1421def6bfb59336a95460fc2329?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ff5e885ce365c2b895df63b9b0d98a7f1a1d1421def6bfb59336a95460fc2329?s=96&d=mm&r=g\",\"caption\":\"Tommy Maynard\"},\"logo\":{\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/ff5e885ce365c2b895df63b9b0d98a7f1a1d1421def6bfb59336a95460fc2329?s=96&d=mm&r=g\"},\"description\":\"Windows PowerShell enthusiast with over 15 years of Information Technology experience.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PowerShell Nested Switch Statements - tommymaynard.com","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:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/","og_locale":"en_US","og_type":"article","og_title":"PowerShell Nested Switch Statements - tommymaynard.com","og_description":"There was a Reddit post recently that gave me an opportunity to try something I&#8217;m not sure I&#8217;ve tried before: nested switch statements\u2014-switch statements inside switch statements. So we&#8217;re on the same page, I&#8217;ve written an example of a basic, non-nested switch. It provides some of the same functionality as an If-ElseIf. I&#8217;ll typically use [&hellip;]","og_url":"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/","og_site_name":"tommymaynard.com","article_published_time":"2016-04-16T06:23:19+00:00","article_modified_time":"2022-12-26T02:09:08+00:00","author":"Tommy Maynard","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Tommy Maynard","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/#article","isPartOf":{"@id":"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/"},"author":{"name":"Tommy Maynard","@id":"https:\/\/tommymaynard.com\/#\/schema\/person\/c74505a44d3d93dc7f82a08502d81ff8"},"headline":"PowerShell Nested Switch Statements","datePublished":"2016-04-16T06:23:19+00:00","dateModified":"2022-12-26T02:09:08+00:00","mainEntityOfPage":{"@id":"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/"},"wordCount":772,"commentCount":0,"publisher":{"@id":"https:\/\/tommymaynard.com\/#\/schema\/person\/c74505a44d3d93dc7f82a08502d81ff8"},"keywords":["If-ElseIf","Switch","Write-Output"],"articleSection":["Quick Learn"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/","url":"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/","name":"PowerShell Nested Switch Statements - tommymaynard.com","isPartOf":{"@id":"https:\/\/tommymaynard.com\/#website"},"datePublished":"2016-04-16T06:23:19+00:00","dateModified":"2022-12-26T02:09:08+00:00","breadcrumb":{"@id":"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/tommymaynard.com\/powershell-nested-switch-statements-2016\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tommymaynard.com\/"},{"@type":"ListItem","position":2,"name":"PowerShell Nested Switch Statements"}]},{"@type":"WebSite","@id":"https:\/\/tommymaynard.com\/#website","url":"https:\/\/tommymaynard.com\/","name":"tommymaynard.com","description":"A PowerShell Resource. Since June 2014.","publisher":{"@id":"https:\/\/tommymaynard.com\/#\/schema\/person\/c74505a44d3d93dc7f82a08502d81ff8"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tommymaynard.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/tommymaynard.com\/#\/schema\/person\/c74505a44d3d93dc7f82a08502d81ff8","name":"Tommy Maynard","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ff5e885ce365c2b895df63b9b0d98a7f1a1d1421def6bfb59336a95460fc2329?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ff5e885ce365c2b895df63b9b0d98a7f1a1d1421def6bfb59336a95460fc2329?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ff5e885ce365c2b895df63b9b0d98a7f1a1d1421def6bfb59336a95460fc2329?s=96&d=mm&r=g","caption":"Tommy Maynard"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/ff5e885ce365c2b895df63b9b0d98a7f1a1d1421def6bfb59336a95460fc2329?s=96&d=mm&r=g"},"description":"Windows PowerShell enthusiast with over 15 years of Information Technology experience."}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p8mce3-18D","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/tommymaynard.com\/wp-json\/wp\/v2\/posts\/4379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tommymaynard.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tommymaynard.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tommymaynard.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tommymaynard.com\/wp-json\/wp\/v2\/comments?post=4379"}],"version-history":[{"count":2,"href":"https:\/\/tommymaynard.com\/wp-json\/wp\/v2\/posts\/4379\/revisions"}],"predecessor-version":[{"id":13578,"href":"https:\/\/tommymaynard.com\/wp-json\/wp\/v2\/posts\/4379\/revisions\/13578"}],"wp:attachment":[{"href":"https:\/\/tommymaynard.com\/wp-json\/wp\/v2\/media?parent=4379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tommymaynard.com\/wp-json\/wp\/v2\/categories?post=4379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tommymaynard.com\/wp-json\/wp\/v2\/tags?post=4379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}