0
0
PowerShellscripting~10 mins

Switch statement in PowerShell - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a switch statement for the variable $color.

PowerShell
switch ([1]) { 'red' { Write-Output 'Stop' } }
Drag options to blanks, or click blank then click option'
A$color
B$value
C$item
D$input
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that does not exist like $value or $item.
Forgetting the $ sign before the variable name.
2fill in blank
medium

Complete the code to output 'Go' when $color is green.

PowerShell
switch ($color) { 'green' { [1] 'Go' } }
Drag options to blanks, or click blank then click option'
APrint
BWrite-Host
CWrite-Output
DEcho
Attempts:
3 left
💡 Hint
Common Mistakes
Using Print which is not a PowerShell command.
Using Echo which is an alias but less clear.
3fill in blank
hard

Fix the error in the switch statement to correctly handle multiple values.

PowerShell
switch ($day) { [1] { Write-Output 'Weekend' } default { Write-Output 'Weekday' } }
Drag options to blanks, or click blank then click option'
A'Saturday'; 'Sunday'
B'Saturday', 'Sunday'
C'Saturday' 'Sunday'
D'Saturday'|'Sunday'
Attempts:
3 left
💡 Hint
Common Mistakes
Using spaces or semicolons instead of commas to separate values.
Using the pipe character which is not valid here.
4fill in blank
hard

Fill both blanks to complete the switch that outputs messages for fruit colors.

PowerShell
switch ($fruit) { [1] { Write-Output 'Yellow fruit' } [2] { Write-Output 'Red fruit' } }
Drag options to blanks, or click blank then click option'
A'banana'
B'apple'
C'cherry'
D'lemon'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing fruit names with wrong colors.
Using fruits not listed in options.
5fill in blank
hard

Fill all three blanks to create a switch that outputs messages for traffic light colors.

PowerShell
switch ([1]) { [2] { Write-Output 'Stop' } [3] { Write-Output 'Go' } default { Write-Output 'Caution' } }
Drag options to blanks, or click blank then click option'
A$light
B'red'
C'green'
D'yellow'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable name.
Mixing up color cases.