Recall & Review
beginner
What is a
switch statement in PowerShell?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:<br>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:<br>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?
✗ 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?
✗ 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?
✗ Incorrect
Use
default { } inside the switch block for unmatched cases.Which option allows a PowerShell switch to match multiple cases?
✗ 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?✗ Incorrect
The correct syntax is
switch ($color) { 'red' { } 'blue' { } }.Explain how a switch statement works in PowerShell and when you might use it.
Think about choosing actions based on different options.
You got /4 concepts.
Write a simple PowerShell switch statement that prints 'Yes' if input is 'y', 'No' if 'n', and 'Unknown' otherwise.
Use default to catch all other inputs.
You got /4 concepts.