PowerShell Switch Statement
The switch statement evaluates a single expression and matches it against multiple possible values. It is a cleaner alternative to long chains of if-elseif-else blocks when checking a variable against many specific values. PowerShell's switch is more powerful than in most other languages — it supports wildcards, regular expressions, and script block conditions.
Why Use Switch Instead of if-elseif
if-elseif (Long and repetitive) switch (Compact and readable)
-------------------------------- ----------------------------
if ($day -eq "Mon") { ... } switch ($day) {
elseif ($day -eq "Tue") { ... } "Mon" { ... }
elseif ($day -eq "Wed") { ... } "Tue" { ... }
elseif ($day -eq "Thu") { ... } "Wed" { ... }
... "Thu" { ... }
}
Basic Switch Syntax
switch (expression) {
value1 { # code block }
value2 { # code block }
value3 { # code block }
Default { # runs if no match found }
}
Simple Switch Example
$day = "Wednesday"
switch ($day) {
"Monday" { Write-Host "Start of the work week" }
"Wednesday" { Write-Host "Mid-week checkpoint" }
"Friday" { Write-Host "End of the work week" }
Default { Write-Host "Just another day" }
}
Output:
Mid-week checkpoint
Switch with Numbers
$statusCode = 404
switch ($statusCode) {
200 { Write-Host "OK – Request successful" }
301 { Write-Host "Moved Permanently" }
400 { Write-Host "Bad Request" }
401 { Write-Host "Unauthorized" }
403 { Write-Host "Forbidden" }
404 { Write-Host "Not Found" }
500 { Write-Host "Internal Server Error" }
Default { Write-Host "Unknown status code: $statusCode" }
}
Output:
Not Found
Switch with Multiple Values Matching the Same Block
$input = "YES"
switch ($input) {
"Y" { Write-Host "User confirmed"; break }
"YES" { Write-Host "User confirmed"; break }
"N" { Write-Host "User declined"; break }
"NO" { Write-Host "User declined"; break }
Default { Write-Host "Invalid input" }
}
Output:
User confirmed
The break Keyword
In PowerShell's switch, all matching cases run by default — there is no automatic stop after the first match. The break keyword stops execution after the first match.
$num = 2
# Without break – all matching cases run
switch ($num) {
2 { Write-Host "Matched 2" }
2 { Write-Host "Also matched 2 again" }
3 { Write-Host "Matched 3" }
}
Output (without break):
Matched 2
Also matched 2 again
# With break – stops after first match
switch ($num) {
2 { Write-Host "Matched 2"; break }
2 { Write-Host "Also matched 2 again"; break }
3 { Write-Host "Matched 3"; break }
}
Output (with break):
Matched 2
Case-Insensitive Matching (Default Behavior)
$color = "RED"
switch ($color) {
"red" { Write-Host "Color is red" }
"green" { Write-Host "Color is green" }
"blue" { Write-Host "Color is blue" }
}
Output:
Color is red
Switch matching is case-insensitive by default. "RED" matches the "red" case without any special setting.
Case-Sensitive Switch with -CaseSensitive
$code = "PS"
switch -CaseSensitive ($code) {
"ps" { Write-Host "Lowercase match" }
"PS" { Write-Host "Uppercase match" }
}
Output:
Uppercase match
Switch with Wildcards (-Wildcard)
Use -Wildcard to match patterns with * and ? wildcards.
$filename = "invoice_2026_march.xlsx"
switch -Wildcard ($filename) {
"invoice_*" { Write-Host "This is an invoice file" }
"report_*" { Write-Host "This is a report file" }
"*.xlsx" { Write-Host "This is an Excel file" }
"*.pdf" { Write-Host "This is a PDF file" }
}
Output:
This is an invoice file
This is an Excel file
Both cases match. Without break, all matching blocks run.
Switch with Regular Expressions (-Regex)
$input = "user@company.com"
switch -Regex ($input) {
"^\d+$" { Write-Host "Input is a number" }
"@" { Write-Host "Input looks like an email" }
"^[A-Z][a-z]+" { Write-Host "Input is a capitalized word" }
Default { Write-Host "Unknown format" }
}
Output:
Input looks like an email
Switch with Script Block Conditions
Each case in a switch can use a script block { } as the condition. This allows complex conditions beyond simple value matching.
$score = 72
switch ($score) {
{ $_ -ge 90 } { Write-Host "Grade: A" }
{ $_ -ge 80 -and $_ -lt 90 } { Write-Host "Grade: B" }
{ $_ -ge 70 -and $_ -lt 80 } { Write-Host "Grade: C" }
{ $_ -ge 60 -and $_ -lt 70 } { Write-Host "Grade: D" }
Default { Write-Host "Grade: F" }
}
Output:
Grade: C
Switch with an Array
When an array is passed to switch, it evaluates each element individually.
$servers = @("WebServer", "DBServer", "CacheServer", "Unknown")
switch ($servers) {
"WebServer" { Write-Host "$_ → Web Tier" }
"DBServer" { Write-Host "$_ → Database Tier" }
"CacheServer" { Write-Host "$_ → Cache Tier" }
Default { Write-Host "$_ → Unclassified" }
}
Output:
WebServer → Web Tier
DBServer → Database Tier
CacheServer → Cache Tier
Unknown → Unclassified
Switch Options Summary
| Option | Purpose | Example Syntax |
|---|---|---|
| (none) | Exact match, case-insensitive | switch ($x) { ... } |
| -CaseSensitive | Exact match, case-sensitive | switch -CaseSensitive ($x) { ... } |
| -Wildcard | Pattern match with * and ? | switch -Wildcard ($x) { ... } |
| -Regex | Regular expression match | switch -Regex ($x) { ... } |
| -File | Read input from a file line by line | switch -File "input.txt" { ... } |
Switch with -File (Process File Line by Line)
# File content: commands.txt
# START
# STOP
# RESTART
# STATUS
switch -File "C:\commands.txt" {
"START" { Write-Host "Starting service..." }
"STOP" { Write-Host "Stopping service..." }
"RESTART" { Write-Host "Restarting service..." }
Default { Write-Host "Unknown command: $_" }
}
Real-World Example – Environment Configuration
$env = "staging"
$dbServer = switch ($env) {
"development" { "localhost" }
"staging" { "staging-db.internal" }
"production" { "prod-db.internal" }
Default { throw "Unknown environment: $env" }
}
Write-Host "Connecting to: $dbServer"
Output:
Connecting to: staging-db.internal
Summary
The switch statement replaces long if-elseif chains with a clean, structured block. PowerShell's switch supports exact match, wildcard, regular expression, and script block conditions. The -CaseSensitive, -Wildcard, and -Regex flags extend its matching capabilities well beyond simple equality checks. Always use break when only the first matching case should run. Switch is particularly useful for routing logic, environment configurations, command parsing, and HTTP status code handling.
