A switch statement lets you test a value against multiple conditions. It runs the code block for the first matching case. It's like choosing a path based on different options.
Click to reveal answer
beginner
How do you write a basic switch statement in PowerShell?
Use the keyword switch followed by parentheses with the value to check. Inside curly braces, list cases with their code blocks. Example:
switch ($value) {
'a' { Write-Output 'Got a' }
'b' { Write-Output 'Got b' }
}
Click to reveal answer
beginner
What happens if no case matches in a PowerShell switch statement?
If no case matches, the switch statement does nothing and moves on. You can add a default case to handle unmatched values.
Click to reveal answer
intermediate
Can a PowerShell switch statement handle multiple matches?
Yes! PowerShell switch can process multiple matches if you use the -Regex or -File options. By default, it stops after the first match.
Click to reveal answer
beginner
How do you add a default case in a PowerShell switch statement?
Use the keyword default inside the switch block. It runs if no other case matches. Example:
switch ($value) {
'a' { Write-Output 'Got a' }
default { Write-Output 'No match found' }
}
Click to reveal answer
What keyword starts a switch statement in PowerShell?
Aswitch
Bcase
Cselect
Dif
✗ Incorrect
The keyword switch starts the switch statement in PowerShell.
What happens if no case matches and there is no default in a PowerShell switch?
AIt throws an error
BIt runs the last case
CIt repeats the switch
DIt does nothing and continues
✗ Incorrect
If no case matches and no default is given, the switch does nothing and continues.
How do you write a default case in a PowerShell switch?
Aelse { }
Bdefault { }
Ccase default { }
Dotherwise { }
✗ Incorrect
Use default { } inside the switch block for unmatched cases.
Which option allows a PowerShell switch to match multiple cases?
A-All
B-Multiple
C-Regex
D-MatchAll
✗ Incorrect
The -Regex option lets switch match multiple cases using patterns.
What is the correct syntax to check a variable $color for 'red' or 'blue' using switch?
PowerShell requires parentheses around the variable in switch: switch ($color) { ... }
Step 2: Verify other parts
Quotes around case values and default case are correct; curly braces are used properly.
Final Answer:
Missing parentheses around $color -> Option B
Quick Check:
Parentheses required around variable = A [OK]
Hint: Always wrap the switch variable in parentheses [OK]
Common Mistakes:
Omitting parentheses around the switch variable
Thinking quotes are optional for strings
Confusing default case necessity
5. You want to write a switch statement that prints 'Weekend' for Saturday and Sunday, 'Weekday' for Monday to Friday, and 'Invalid' for any other input. Which code snippet correctly implements this?
Each case must have exactly one pattern followed by { action }. Multiple values require separate case statements.
Step 2: Check each option
A lists a separate case for each day: correct syntax and produces expected output. B uses -or which evaluates to $true (boolean) and won't match string days. C and D use commas to attempt grouping, but this causes syntax errors (unexpected token ',').