Many developers transitioning from VBScript to PowerShell expect similar switch statement behavior, particularly the ability to handle multiple values in a single case using comma-separated syntax. While PowerShell's switch is more powerful overall, this specific functionality requires a different approach.
Here are three effective methods to achieve the VBScript-style multi-value matching:
# Method 1: Using script blocks with -in operator (PowerShell 3.0+)
switch ($myNumber) {
{ $_ -in 1,3,5,7,9 } { "Odd" }
{ $_ -in 2,4,6,8,10 } { "Even" }
}
# Method 2: Using regex pattern matching
switch -regex ($myNumber) {
'^(1|3|5|7|9)$' { "Odd" }
'^(2|4|6|8|10)$' { "Even" }
}
# Method 3: Traditional multiple cases (verbose but clear)
switch ($myNumber) {
1 { "Odd" }
3 { "Odd" }
5 { "Odd" }
7 { "Odd" }
9 { "Odd" }
2 { "Even" }
4 { "Even" }
6 { "Even" }
8 { "Even" }
10 { "Even" }
}
For more complex scenarios, you can predefine arrays and check containment:
$oddNumbers = 1,3,5,7,9
$evenNumbers = 2,4,6,8,10
switch ($myNumber) {
{ $oddNumbers -contains $_ } { "Odd" }
{ $evenNumbers -contains $_ } { "Even" }
default { "Out of range" }
}
The -in operator (Method 1) generally offers the best balance between readability and performance for most use cases. The regex approach (Method 2) becomes more efficient when dealing with very large value sets or complex patterns.
Here's how you might classify HTTP status codes using this technique:
switch ($statusCode) {
{ $_ -in 200,201,202,203,204 } { "Success" }
{ $_ -in 301,302,303,307,308 } { "Redirection" }
{ $_ -in 400,401,403,404,422 } { "Client Error" }
{ $_ -in 500,501,502,503,504 } { "Server Error" }
default { "Unknown status code" }
}
Many developers transitioning from VBScript to PowerShell often wonder how to handle multiple matching values in switch statements. In VBScript, you could simply comma-separate values in a Case block, but PowerShell handles this differently.
PowerShell's switch statement is actually more powerful than VBScript's Select Case, but the syntax differs significantly. Here's the basic structure:
switch ($variable) {
value1 { action1 }
value2 { action2 }
default { defaultAction }
}
There are several ways to handle multiple values in PowerShell switch statements:
Method 1: Script Block Conditions
The most PowerShell-like approach uses script blocks with the -in operator:
switch ($myNumber) {
{ $_ -in 1,3,5,7,9 } { "Odd" }
{ $_ -in 2,4,6,8,10 } { "Even" }
}
Method 2: Regex Pattern Matching
For numeric patterns, regular expressions can be concise:
switch -regex ($myNumber) {
"^[13579]$" { "Odd" }
"^[24680]$" { "Even" }
}
Method 3: Using Arrays and Contains
Another readable approach using arrays:
$odds = @(1,3,5,7,9)
$evens = @(2,4,6,8,10)
switch ($myNumber) {
{ $odds -contains $_ } { "Odd" }
{ $evens -contains $_ } { "Even" }
}
For string values, wildcards can be powerful:
switch -wildcard ($myString) {
"error*" { "Error detected" }
"warning*" { "Warning found" }
}
When dealing with large value sets:
- Arrays (-contains) perform better for 10+ values
- Regex becomes efficient for pattern-based matches
- Hash tables might be better for very large value sets
Here's a practical example handling HTTP status codes:
switch ($statusCode) {
{ $_ -in 200,201,202 } { "Success" }
{ $_ -in 400,401,403 } { "Client Error" }
{ $_ -in 500,502,503 } { "Server Error" }
default { "Unknown Status" }
}