Power Shell | Inside Powershell

Inhaltsverzeichnis

Language

Switches

Basic Syntax

Let’s start by taking a look at a basic switch statement.

$i = 1;
switch ($i)
{
1 {
write-host “one”
break
}
2 {
write-host “two”
write-host “two”
break
}
default {
write-host “other”
break
}
}

Notice we don’t use the Case: keyword at all.

The default in PowerShell is to assume -eq is used between your input and typed case value. This means No Case Sensitivity and No Wild Cards:

switch(“hello”) #will match uppercase
{
“HELLO” {write-host “Uppercase” -ForegroundColor Magenta;break}
“hello” {write-host “lowercase” -ForegroundColor green;break}
}
switch(“hello”) #will NOT match h*
{
“h*” {write-host “wildcard” -ForegroundColor Magenta;break}
“hello” {write-host “lowercase” -ForegroundColor green;break}
}

Expression Match

We have a long-hand switch statement that lets us use whatever we want (-eq, -gt, -le, -like, straight up boolean values, etc.)

Psudo-code:Switch($value) { {<bool expression>} {<code>} {<bool expression>} {<code>} default {<code>} }

Real:

switch(“hello”) #will match uppercase
{
“HELLO” {write-host “Uppercase” -ForegroundColor Magenta;break}
“hello” {write-host “lowercase” -ForegroundColor green;break}
}
switch(“hello”) #will NOT match h*
{
“h*” {write-host “wildcard” -ForegroundColor Magenta;break}
“hello” {write-host “lowercase” -ForegroundColor green;break}
}

Jump Statements aren’t necessary

C# requires jump statements such as break, goto, or return. PowerShell does not!

This is one of the coolest features in PowerShell. We actually allow for continuous case- checks.

This means your switches can actually act more like a bunch of independent if statements. Notice the previous example, without any of the “break statements” and using a number that is less than 5, 10 and 15.

$num = 4
Switch($num)
{
{$num -lt 5} {write-host “less than 5!” -ForegroundColor Magenta}
{$num -lt 10} {write-host “less than 10!” -ForegroundColor green}
{$num -lt 15} {write-host “less than 15!” -ForegroundColor cyan}
default {write-host “greater than or equal to 15” -ForegroundColor yellow}
}

Loops and $_

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:

$nums = 1..15
foreach($num in $nums)
{
Switch($num)
{
{$num -lt 5} {write-host “$num is less than 5!” -ForegroundColor Magenta}
{$num -lt 10} {write-host “$num is less than 10!” -ForegroundColor green}
{$num -lt 15} {write-host “$num is less than 15!” -ForegroundColor cyan}
default {write-host “$num is greater than or equal to 15” -ForegroundColor yellow}
}
}

However, PowerShell actually has a loop and $_ built right into your switch so we can chop off the foreach completely:

$nums = 1..15
Switch($nums)
{
{$_ -lt 5} {write-host “$_ is less than 5!” -ForegroundColor Magenta}
{$_ -lt 10} {write-host “$_ is less than 10!” -ForegroundColor green}
{$_ -lt 15} {write-host “$_ is less than 15!” -ForegroundColor cyan}
default {write-host “$_ is greater than or equal to 15” -ForegroundColor yellow}
}

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’s an empty collection it will just skip the whole switch!

This, however, can lead to some confusion if you try to use “break” since our loop is also the whole switch statement:

$nums = 1..15
Switch($nums)
{
{$_ -lt 5} {write-host “$_ is less than 5!” -ForegroundColor Magenta;break}
{$_ -lt 10} {write-host “$_ is less than 10!” -ForegroundColor green;break}
{$_ -lt 15} {write-host “$_ is less than 15!” -ForegroundColor cyan;break}
default {write-host “$_ is greater than or equal to 15” -ForegroundColor yellow}
}

view rawLoopSwitchSample3.ps1 hosted with ❤ by GitHub

Uh-oh, not good.

We also have “continue” 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:

$nums = 1..15
Switch($nums)
{
{$_ -lt 5} {write-host “$_ is less than 5!” -ForegroundColor Magenta;continue}
{$_ -lt 10} {write-host “$_ is less than 10!” -ForegroundColor green;continue}
{$_ -lt 15} {write-host “$_ is less than 15!” -ForegroundColor cyan;continue}
default {write-host “$_ is greater than or equal to 15” -ForegroundColor yellow}
}

Shortcuts

In addition to the looping, we provide you a few other handy short cuts.

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.

Notice, weirdly, the parameters must come between the word “switch” and the parenthesis, they won’t work at the end of the parenthesis.

switch -casesensitive (“hello”) #makes it a -ceq
{
“HELLO” {write-host “Uppercase” -ForegroundColor Magenta}
“hello” {write-host “lowercase” -ForegroundColor green}
“h*” {write-host “wildcard” -ForegroundColor Magenta}
}
switch -wildcard (“hello”) #makes it a -like
{
“HELLO” {write-host “Uppercase” -ForegroundColor Magenta}
“hello” {write-host “lowercase” -ForegroundColor green}
“h*” {write-host “wildcard” -ForegroundColor Magenta}
}
switch -wildcard -CaseSensitive (“hello”) #makes it a -clike
{
“HELLO” {write-host “Uppercase” -ForegroundColor Magenta}
“hello” {write-host “lowercase” -ForegroundColor green}
“h*” {write-host “wildcard” -ForegroundColor Magenta}
}
switch -regex (“hello”) #makes it a -match
{
“\w{5}” {write-host “5 characters” -ForegroundColor Magenta}
“\w{6}” {write-host “6 characters” -ForegroundColor green}
}

CommandLets

  • Get-Alias
  • New-Alias
  • Set-Alias
  • Export-Alias
  • Import-Alias 

Function

To see the definition of mkdir use

Get-Content Function:\mkdir