{"id":345,"date":"2018-03-14T00:00:38","date_gmt":"2018-03-14T08:00:38","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/koryt\/?p=345"},"modified":"2020-04-29T10:16:13","modified_gmt":"2020-04-29T17:16:13","slug":"powershell-for-programmers-the-magic-switch","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-for-programmers-the-magic-switch\/","title":{"rendered":"PowerShell for Programmers: The Magic Switch!"},"content":{"rendered":"<p>Welcome back everyone, I&#8217;m trying out <a href=\"https:\/\/gist.github.com\/\">GitHub Gist<\/a>\u00a0for my code blocks this week. It lets you click and download them, as well as making them easy to edit. The downside is that I can&#8217;t use my usual dark themed syntax highlighting. Let me know in the comments if you like gist or the old method better \ud83d\ude42<\/p>\n<p>Switch statements in PowerShell are very cool, and by knowing some of the tricks it has in place it can save you a lot of time.<\/p>\n<h2>Basic Syntax<\/h2>\n<p>Let&#8217;s start by taking a look at a basic switch statement.<\/p>\n<p>In C# you might write something like this:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Sambardo\/10d1611010703814ab4357b8be249957.js\"><\/script><\/p>\n<p>In PowerShell you could do this:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Sambardo\/c026f4943474ec846c34ebbbfae49314.js\"><\/script><\/p>\n<p><u>Notice we don&#8217;t use the Case: keyword at all.<\/u><\/p>\n<p>The default in PowerShell is to <em>assume -eq is used<\/em> between your input and typed case value. This means <u>No Case Sensitivity and No Wild Cards:<\/u><\/p>\n<p><script src=\"https:\/\/gist.github.com\/Sambardo\/9e05630c79ea65306279a0260c52c54c.js\"><\/script><\/p>\n<h2>Expression Match<\/h2>\n<p>We have a long-hand switch statement that lets us use whatever we want (-eq, -gt, -le, -like, straight up boolean values, etc.)<\/p>\n<p>Psudo-code:<\/p>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:e01843a1-ccd5-4682-822d-918b897b9b73\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; padding: 0px; float: none;\">\n<pre class=\"lang:default decode:true\">Switch($value)\r\n{\r\n{&lt;bool expression&gt;} {&lt;code&gt;}\r\n{&lt;bool expression&gt;} {&lt;code&gt;}\r\ndefault {&lt;code&gt;}\r\n}<\/pre>\n<\/div>\n<p>Real:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Sambardo\/9e05630c79ea65306279a0260c52c54c.js\"><\/script><\/p>\n<h2>Jump Statements aren&#8217;t necessary<\/h2>\n<p>C# requires jump statements such as break, goto, or return. PowerShell does not!<\/p>\n<p>This is one of the coolest features in PowerShell. We actually allow for continuous case- checks.<\/p>\n<p>This means your switches can actually act more like a bunch of independent if statements. Notice the previous example, without any of the &#8220;break statements&#8221; and using a number that is less than 5, 10 and 15.\n<script src=\"https:\/\/gist.github.com\/Sambardo\/cbc27d934bf40284f6af13171e0164cf.js\"><\/script><\/p>\n<blockquote><p>How cool is that?<\/p><\/blockquote>\n<p>Very cool.<\/p>\n<h2>Loops and $_<\/h2>\n<p>It might be common for you to take a bunch of data, do a foreach loop through it and send each value through your switch:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Sambardo\/646c7f759f3e3859d33d4da47e258833.js\"><\/script><\/p>\n<p>However, PowerShell actually has a loop and $_ built right into your switch so we can chop off the foreach completely:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Sambardo\/3b85af2a8fce8858aa97834bdc5ac9c8.js\"><\/script><\/p>\n<p>This lets us write some really concise and convenient little code blocks. The nice thing is that if our list has 1 object it still gets handled fine, and if it&#8217;s an empty collection it will just skip the whole switch!<\/p>\n<p>This, however, can lead to some confusion if you try to use &#8220;break&#8221; since our loop is also the whole switch statement:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Sambardo\/c11291b009f98d634dba0490bf2197c8.js\"><\/script><\/p>\n<blockquote><p>Uh-oh, not good.<\/p><\/blockquote>\n<p>We also have &#8220;continue&#8221; in PowerShell and this will stop our current iteration of our loop (or switch) so we can use the looping feature and make it like a bunch of elseifs:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Sambardo\/5c89c6bfebffe5c03a432f10bd3d5064.js\"><\/script><\/p>\n<h2>Shortcuts<\/h2>\n<p>In addition to the looping, we provide you a few other handy short cuts.<\/p>\n<p>If you just wanted a basic equality switch, but would want to use -ceq (case sensitivity), -like (wild cards), or -match (regex) we let you do that without writing an expression match via some parameters.<\/p>\n<p>Notice, weirdly, the parameters must come between the word &#8220;switch&#8221; and the parenthesis, they won&#8217;t work at the end of the parenthesis.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Sambardo\/6201cea224385a1179f84caf8289d6ec.js\"><\/script><\/p>\n<p>Well that\u2019s all for now, hopefully this helps you do some cool shortcuts with switches and get some nice, easy to read code into your scripts!<\/p>\n<p>For the main series post, check back\u00a0<a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-for-programmers-a-quick-start-guide\/\">here.<\/a><\/p>\n<p>If you find this helpful don&#8217;t forget to rate, comment and share\u00a0\ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome back everyone, I&#8217;m trying out GitHub Gist\u00a0for my code blocks this week. It lets you click and download them, as well as making them easy to edit. The downside is that I can&#8217;t use my usual dark themed syntax highlighting. Let me know in the comments if you like gist or the old method [&hellip;]<\/p>\n","protected":false},"author":7300,"featured_media":87096,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1738,2126],"tags":[2221,2125],"class_list":["post-345","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","category-powershell_for_programmers","tag-kory-thacher","tag-koryt"],"acf":[],"blog_post_summary":"<p>Welcome back everyone, I&#8217;m trying out GitHub Gist\u00a0for my code blocks this week. It lets you click and download them, as well as making them easy to edit. The downside is that I can&#8217;t use my usual dark themed syntax highlighting. Let me know in the comments if you like gist or the old method [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/345","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\/7300"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=345"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/345\/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=345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}