r/PowerShell • • Jul 02 '20

Chapter 8 - Switch Statements

https://youtu.be/JcrLNVwplAI
119 Upvotes

19 comments sorted by

View all comments

2

u/jsiii2010 Jul 02 '20 edited Jul 02 '20

Switch is the Thor of powershell commands. Notes from Windows Powershell in Action.

switch -wildcard ('abc') {a* {"a*: $_"} *c {"*c: $_"}}

switch -regex ('abc') {'(^a)(.*$)' {$matches}}

switch (8) {
  {$_ -gt 3} {'greater than three'}
  {$_ -gt 7} {'greater than 7'}
}

switch(1..6) {
  {$_ % 2} {"Odd $_"; continue}
  4 {"FOUR $_"}
  default {"Even $_"}
}

switch -wildcard (Get-ChildItem c:\windows) {
  *.dll {$dll++}
  *.txt {$txt++}
  *.log {$log++}
}
"dll $dll txt $txt log $log"

switch ($options){
  '-a' { $a=$true }
  '-b' { [void] $switch.MoveNext(); $b= $switch.Current }
  '-c' { $c=$true }
  '-d' { $d=$true }
}

2

u/mistajingsta Jul 02 '20

switch -wildcard ('abc') {a* {"a*: $"} *c {"*c: $"}}

switch -regex ('abc') {'(a)(.*$)' {$matches}}

switch (8) { {$_ -gt 3} {'greater than three'} {$_ -gt 7} {'greater than 7'} }

switch(1..6) { {$_ % 2} {"Odd $"; continue} 4 {"FOUR $"} default {"Even $_"} }

switch -wildcard (Get-ChildItem c:\windows) { *.dll {$dll++} *.txt {$txt++} *.log {$log++} } "dll $dll txt $txt log $log"

switch ($options){ '-a' { $a=$true } '-b' { [void] $switch.MoveNext(); $b= $switch.Current } '-c' { $c=$true } '-d' { $d=$true } }

Ill add that to the doc!