{"id":9584,"date":"2023-08-02T10:29:01","date_gmt":"2023-08-02T08:29:01","guid":{"rendered":"https:\/\/via-internet.de\/blog\/?p=9584"},"modified":"2023-08-10T09:30:44","modified_gmt":"2023-08-10T07:30:44","slug":"power-shell-inside-powershell","status":"publish","type":"post","link":"https:\/\/via-internet.de\/blog\/2023\/08\/02\/power-shell-inside-powershell\/","title":{"rendered":"Power Shell | Inside Powershell"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Language<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Switches<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Basic Syntax<a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-for-programmers-the-magic-switch\/#basic-syntax\"><\/a><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s start by taking a look at a basic switch statement.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"><\/pre>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>$i = 1;<\/td><\/tr><tr><td><\/td><\/tr><tr><td>switch ($i)<\/td><\/tr><tr><td>{<\/td><\/tr><tr><td>1 {<\/td><\/tr><tr><td>write-host &#8220;one&#8221;<\/td><\/tr><tr><td>break<\/td><\/tr><tr><td>}<\/td><\/tr><tr><td>2 {<\/td><\/tr><tr><td>write-host &#8220;two&#8221;<\/td><\/tr><tr><td>write-host &#8220;two&#8221;<\/td><\/tr><tr><td>break<\/td><\/tr><tr><td>}<\/td><\/tr><tr><td>default {<\/td><\/tr><tr><td>write-host &#8220;other&#8221;<\/td><\/tr><tr><td>break<\/td><\/tr><tr><td>}<\/td><\/tr><tr><td>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Notice we don\u2019t use the Case: keyword at all.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The default in PowerShell is to&nbsp;<em>assume -eq is used<\/em>&nbsp;between your input and typed case value. This means&nbsp;No Case Sensitivity and No Wild Cards:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>switch(&#8220;hello&#8221;) #will match uppercase<\/td><\/tr><tr><td>{<\/td><\/tr><tr><td>&#8220;HELLO&#8221; {write-host &#8220;Uppercase&#8221; -ForegroundColor Magenta;break}<\/td><\/tr><tr><td>&#8220;hello&#8221; {write-host &#8220;lowercase&#8221; -ForegroundColor green;break}<\/td><\/tr><tr><td>}<\/td><\/tr><tr><td><\/td><\/tr><tr><td>switch(&#8220;hello&#8221;) #will NOT match h*<\/td><\/tr><tr><td>{<\/td><\/tr><tr><td>&#8220;h*&#8221; {write-host &#8220;wildcard&#8221; -ForegroundColor Magenta;break}<\/td><\/tr><tr><td>&#8220;hello&#8221; {write-host &#8220;lowercase&#8221; -ForegroundColor green;break}<\/td><\/tr><tr><td>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"expression-match\">Expression Match<a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-for-programmers-the-magic-switch\/#expression-match\"><\/a><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">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\n\n\n<p class=\"wp-block-paragraph\">Psudo-code:Switch($value) { {&lt;bool expression&gt;} {&lt;code&gt;} {&lt;bool expression&gt;} {&lt;code&gt;} default {&lt;code&gt;} }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Real:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>switch(&#8220;hello&#8221;) #will match uppercase<\/td><\/tr><tr><td>{<\/td><\/tr><tr><td>&#8220;HELLO&#8221; {write-host &#8220;Uppercase&#8221; -ForegroundColor Magenta;break}<\/td><\/tr><tr><td>&#8220;hello&#8221; {write-host &#8220;lowercase&#8221; -ForegroundColor green;break}<\/td><\/tr><tr><td>}<\/td><\/tr><tr><td><\/td><\/tr><tr><td>switch(&#8220;hello&#8221;) #will NOT match h*<\/td><\/tr><tr><td>{<\/td><\/tr><tr><td>&#8220;h*&#8221; {write-host &#8220;wildcard&#8221; -ForegroundColor Magenta;break}<\/td><\/tr><tr><td>&#8220;hello&#8221; {write-host &#8220;lowercase&#8221; -ForegroundColor green;break}<\/td><\/tr><tr><td>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"jump-statements-arent-necessary\">Jump Statements aren\u2019t necessary<a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-for-programmers-the-magic-switch\/#jump-statements-arent-necessary\"><\/a><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">C# requires jump statements such as break, goto, or return. PowerShell does not!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is one of the coolest features in PowerShell. We actually allow for continuous case- checks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This means your switches can actually act more like a bunch of independent if statements. Notice the previous example, without any of the \u201cbreak statements\u201d and using a number that is less than 5, 10 and 15.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><\/td><td>$num = 4<\/td><\/tr><tr><td><\/td><td>Switch($num)<\/td><\/tr><tr><td><\/td><td>{<\/td><\/tr><tr><td><\/td><td>{$num -lt 5} {write-host &#8220;less than 5!&#8221; -ForegroundColor Magenta}<\/td><\/tr><tr><td><\/td><td>{$num -lt 10} {write-host &#8220;less than 10!&#8221; -ForegroundColor green}<\/td><\/tr><tr><td><\/td><td>{$num -lt 15} {write-host &#8220;less than 15!&#8221; -ForegroundColor cyan}<\/td><\/tr><tr><td><\/td><td>default {write-host &#8220;greater than or equal to 15&#8221; -ForegroundColor yellow}<\/td><\/tr><tr><td><\/td><td>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"loops-and-_\">Loops and $_<a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-for-programmers-the-magic-switch\/#loops-and-_\"><\/a><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">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\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>$nums = 1..15<\/td><\/tr><tr><td><\/td><\/tr><tr><td>foreach($num in $nums)<\/td><\/tr><tr><td>{<\/td><\/tr><tr><td>Switch($num)<\/td><\/tr><tr><td>{<\/td><\/tr><tr><td>{$num -lt 5} {write-host &#8220;$num is less than 5!&#8221; -ForegroundColor Magenta}<\/td><\/tr><tr><td>{$num -lt 10} {write-host &#8220;$num is less than 10!&#8221; -ForegroundColor green}<\/td><\/tr><tr><td>{$num -lt 15} {write-host &#8220;$num is less than 15!&#8221; -ForegroundColor cyan}<\/td><\/tr><tr><td>default {write-host &#8220;$num is greater than or equal to 15&#8221; -ForegroundColor yellow}<\/td><\/tr><tr><td>}<\/td><\/tr><tr><td>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">However, PowerShell actually has a loop and $_ built right into your switch so we can chop off the foreach completely:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>$nums = 1..15<\/td><\/tr><tr><td>Switch($nums)<\/td><\/tr><tr><td>{<\/td><\/tr><tr><td>{$_ -lt 5} {write-host &#8220;$_ is less than 5!&#8221; -ForegroundColor Magenta}<\/td><\/tr><tr><td>{$_ -lt 10} {write-host &#8220;$_ is less than 10!&#8221; -ForegroundColor green}<\/td><\/tr><tr><td>{$_ -lt 15} {write-host &#8220;$_ is less than 15!&#8221; -ForegroundColor cyan}<\/td><\/tr><tr><td>default {write-host &#8220;$_ is greater than or equal to 15&#8221; -ForegroundColor yellow}<\/td><\/tr><tr><td>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">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\u2019s an empty collection it will just skip the whole switch!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This, however, can lead to some confusion if you try to use \u201cbreak\u201d since our loop is also the whole switch statement:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>$nums = 1..15<\/td><\/tr><tr><td>Switch($nums)<\/td><\/tr><tr><td>{<\/td><\/tr><tr><td>{$_ -lt 5} {write-host &#8220;$_ is less than 5!&#8221; -ForegroundColor Magenta;break}<\/td><\/tr><tr><td>{$_ -lt 10} {write-host &#8220;$_ is less than 10!&#8221; -ForegroundColor green;break}<\/td><\/tr><tr><td>{$_ -lt 15} {write-host &#8220;$_ is less than 15!&#8221; -ForegroundColor cyan;break}<\/td><\/tr><tr><td>default {write-host &#8220;$_ is greater than or equal to 15&#8221; -ForegroundColor yellow}<\/td><\/tr><tr><td>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/gist.github.com\/Sambardo\/c11291b009f98d634dba0490bf2197c8\/raw\/92ab9ff2e39e393f60ca0c626211bec75fd54b1a\/LoopSwitchSample3.ps1\" target=\"_blank\" rel=\"noreferrer noopener\">view raw<\/a><a href=\"https:\/\/gist.github.com\/Sambardo\/c11291b009f98d634dba0490bf2197c8#file-loopswitchsample3-ps1\" target=\"_blank\" rel=\"noreferrer noopener\">LoopSwitchSample3.ps1&nbsp;<\/a>hosted with \u2764 by&nbsp;<a href=\"https:\/\/github.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub<\/a><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">Uh-oh, not good.<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">We also have \u201ccontinue\u201d 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\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>$nums = 1..15<\/td><\/tr><tr><td>Switch($nums)<\/td><\/tr><tr><td>{<\/td><\/tr><tr><td>{$_ -lt 5} {write-host &#8220;$_ is less than 5!&#8221; -ForegroundColor Magenta;continue}<\/td><\/tr><tr><td>{$_ -lt 10} {write-host &#8220;$_ is less than 10!&#8221; -ForegroundColor green;continue}<\/td><\/tr><tr><td>{$_ -lt 15} {write-host &#8220;$_ is less than 15!&#8221; -ForegroundColor cyan;continue}<\/td><\/tr><tr><td>default {write-host &#8220;$_ is greater than or equal to 15&#8221; -ForegroundColor yellow}<\/td><\/tr><tr><td>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"shortcuts\">Shortcuts<a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-for-programmers-the-magic-switch\/#shortcuts\"><\/a><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In addition to the looping, we provide you a few other handy short cuts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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\n\n\n<p class=\"wp-block-paragraph\">Notice, weirdly, the parameters must come between the word \u201cswitch\u201d and the parenthesis, they won\u2019t work at the end of the parenthesis.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>switch -casesensitive (&#8220;hello&#8221;) #makes it a -ceq<\/td><\/tr><tr><td>{<\/td><\/tr><tr><td>&#8220;HELLO&#8221; {write-host &#8220;Uppercase&#8221; -ForegroundColor Magenta}<\/td><\/tr><tr><td>&#8220;hello&#8221; {write-host &#8220;lowercase&#8221; -ForegroundColor green}<\/td><\/tr><tr><td>&#8220;h*&#8221; {write-host &#8220;wildcard&#8221; -ForegroundColor Magenta}<\/td><\/tr><tr><td>}<\/td><\/tr><tr><td><\/td><\/tr><tr><td>switch -wildcard (&#8220;hello&#8221;) #makes it a -like<\/td><\/tr><tr><td>{<\/td><\/tr><tr><td>&#8220;HELLO&#8221; {write-host &#8220;Uppercase&#8221; -ForegroundColor Magenta}<\/td><\/tr><tr><td>&#8220;hello&#8221; {write-host &#8220;lowercase&#8221; -ForegroundColor green}<\/td><\/tr><tr><td>&#8220;h*&#8221; {write-host &#8220;wildcard&#8221; -ForegroundColor Magenta}<\/td><\/tr><tr><td>}<\/td><\/tr><tr><td><\/td><\/tr><tr><td>switch -wildcard -CaseSensitive (&#8220;hello&#8221;) #makes it a -clike<\/td><\/tr><tr><td>{<\/td><\/tr><tr><td>&#8220;HELLO&#8221; {write-host &#8220;Uppercase&#8221; -ForegroundColor Magenta}<\/td><\/tr><tr><td>&#8220;hello&#8221; {write-host &#8220;lowercase&#8221; -ForegroundColor green}<\/td><\/tr><tr><td>&#8220;h*&#8221; {write-host &#8220;wildcard&#8221; -ForegroundColor Magenta}<\/td><\/tr><tr><td>}<\/td><\/tr><tr><td><\/td><\/tr><tr><td>switch -regex (&#8220;hello&#8221;) #makes it a -match<\/td><\/tr><tr><td>{<\/td><\/tr><tr><td>&#8220;\\w{5}&#8221; {write-host &#8220;5 characters&#8221; -ForegroundColor Magenta}<\/td><\/tr><tr><td>&#8220;\\w{6}&#8221; {write-host &#8220;6 characters&#8221; -ForegroundColor green}<\/td><\/tr><tr><td>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">CommandLets<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Get-Alias<\/code><\/li>\n\n\n\n<li><code>New-Alias<\/code><\/li>\n\n\n\n<li><code>Set-Alias<\/code><\/li>\n\n\n\n<li><code>Export-Alias<\/code><\/li>\n\n\n\n<li><code>Import-Alias<\/code>&nbsp;<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Function<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To see the definition of <code data-enlighter-language=\"shell\" class=\"EnlighterJSRAW\">mkdir<\/code> use<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Get-Content Function:\\mkdir<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Language Switches Basic Syntax Let\u2019s start by taking a look at a basic switch statement. $i = 1; switch ($i) { 1 { write-host &#8220;one&#8221; break } 2 { write-host &#8220;two&#8221; write-host &#8220;two&#8221; break } default { write-host &#8220;other&#8221; break } } Notice we don\u2019t use the Case: keyword at all. The default in PowerShell is to&nbsp;assume -eq is used&nbsp;between your input and typed case value. This means&nbsp;No Case Sensitivity and No Wild Cards: switch(&#8220;hello&#8221;) #will match uppercase { &#8220;HELLO&#8221; {write-host &#8220;Uppercase&#8221; -ForegroundColor Magenta;break} &#8220;hello&#8221; {write-host &#8220;lowercase&#8221; -ForegroundColor green;break} } switch(&#8220;hello&#8221;) #will NOT match h* { &#8220;h*&#8221; {write-host &#8220;wildcard&#8221; -ForegroundColor Magenta;break} [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7170,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[102],"tags":[],"class_list":["post-9584","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-power-shell"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/9584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/comments?post=9584"}],"version-history":[{"count":4,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/9584\/revisions"}],"predecessor-version":[{"id":9643,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/9584\/revisions\/9643"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/media?parent=9584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/categories?post=9584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/tags?post=9584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}