switch Statement
noun
say it: “SWICH STAYT-ment” /ˈswɪtʃ ˈsteɪtmənt/
A part of a script that compares one value with a list of choices and runs the commands for each choice that matches.
What is a switch statement in PowerShell?
A switch statement compares one value with a list of choices and runs the block of every choice that matches, with default for anything else. It ignores capital letters, and -Wildcard lets a choice be a pattern such as '*.jpg'.
$choice = 'Q'
switch ($choice) {
'1' { "Back up today's changes" }
'2' { "Back up this week's changes" }
'q' { 'Bye.' }
default { "There's no $choice on the menu." }
}
# Bye.
In a sentence
- “Sam's switch statement has a default block for any choice that isn't on the menu.”
- “The backup menu sends each answer to the right command with a switch statement.”
Level: Apprentice · More PowerShell terms at this level
Updated