Prof. Powershell

Switch Way Did They Go?

Pair up Regex with the Switch construct to get what you need, in plain English.

I know we've had some introductory lessons in the past on the Switch construct. This construct evaluates some expression against a collection of values. When there is a match, the associated scriptblock is executed. Here's a basic example:

$choice="Reboot"
Switch ($choice) {
"Shutdown" {stop-computer -whatif }
"Reboot" {restart-computer -whatif}
"Checkpoint" {Checkpoint-computer -whatif}
"OS" {get-wmiobject -class "win32_operatingsystem"}
Default {Write-Verbose "Invalid choice: $choice"}
}

When this code is executed the value of $choice is compared. Because $choice matches "Reboot", the associated scriptblock, {restart-computer -whatif"}, is executed. The Default scriptblock only executes when there is no match. The Default statement is purely optional. This type of comparison uses simple matching. But you can also tell Switch to use regular expressions.

Regular expressions are an incredibly vast topic. In a nutshell, a regular expression is a way of expressing a pattern. For example, we recognize 192.168.1.3 as an IP address because of the pattern. A regular expression can be used to identify this pattern. Here's how you might use Switch with regular expressions. This is simple demonstration:

$x=Read-host "enter something"
Switch -regex ($x) {
\d+ {
  write-host "You entered one or more numbers: $($matches.item(0))"
  }
\w+ {
  write-host "You entered one or more alpha characters:
  $($matches.item(0))"
  }
\W+ {
  write-host "You entered one or more non-alphanumeric characters:
  $($matches.item(0))"
  }
Default {
  write-host "I don't know what you entered: $x"
  }
}

The Switch construct uses regular expression patterns to "decode" what you enter, like this.

PS C:\work> .\switchdemo.ps1
enter something: Jeff
You entered one or more alpha characters: Jeff

Because "Jeff" matched the "\w+" regular expression, the corresponding script block was executed. But look at this:

PS C:\work> .\switchdemo.ps1
enter something: Jeff123
You entered one or more numbers: 123
You entered one or more alpha characters: Jeff123

The Switch construct executes every matching statement. Depending on your situation this may be perfectly acceptable. Otherwise, you can add a Break statement so that Switch will only execute the first match and then exit.

\d+ {
  write-host "You entered one or more numbers: $($matches.item(0))"
  Break
  }

Or you might want to modify your regular expression pattern. I'll modify mine to anchor the pattern I'm looking for at the beginning,

Switch -regex ($x) {
^\d+ {
  write-host "You entered one or more numbers: $($matches.item(0))"
  }
^\w+ {
  write-host "You entered one or more alpha characters:
  $($matches.item(0))"
  }
^\W+ {
  write-host "You entered one or more non-alphanumeric characters:   $($matches.item(0))"
  }
Default {
  write-host "I don't know what you entered: $x"
  }
}

Now I only get a match when $x starts with alpha characters.

PS C:\work> .\switchdemo.ps1
enter something: Jeff123
You entered one or more alpha characters: Jeff123

So the next time you need a more sophisticated logic structure, take a look at Switch with Regex.

If you want to learn more about Regular Expressions in Windows PowerShell, take a look at Windows PowerShell 2.0: TFM (SAPIEN Press 2010).

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.