xxxxxxxxxx
switch(<test-value>) {
<condition> {<action>}
break; // optional
<condition> {<action>}
break; // optional
<condition> {<action>}
break; // optional
}
xxxxxxxxxx
switch -regex(var) {
"three" {<action>}
"one|two" {<action>}
default {<action>}
}
xxxxxxxxxx
function MyFunctionWithSwitch {
[CmdletBinding()]
param (
[switch]$Verbose
)
# Your function logic here
if ($Verbose) {
Write-Host "Verbose mode is enabled."
} else {
Write-Host "Verbose mode is not enabled."
}
}
# Call the function with the switch
MyFunctionWithSwitch -Verbose
# Call the function without the switch
MyFunctionWithSwitch