0
0
PowerShellscripting~20 mins

Switch statement in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell Switch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a simple switch statement
What is the output of this PowerShell script?
PowerShell
switch (3) {
  1 { 'One' }
  2 { 'Two' }
  3 { 'Three' }
  default { 'Other' }
}
AOther
BThree
C1
DTwo
Attempts:
2 left
💡 Hint
The switch statement matches the value 3 to the case 3.
💻 Command Output
intermediate
2:00remaining
Switch with multiple matching cases
What will this PowerShell script output?
PowerShell
switch (4) {
  {$_ -lt 3} { 'Less than 3' }
  {$_ -lt 5} { 'Less than 5' }
  default { '5 or more' }
}
ALess than 3
B
Less than 3
Less than 5
C5 or more
DLess than 5
Attempts:
2 left
💡 Hint
Switch stops at the first matching case unless -File or -Regex is used.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in switch statement
Which option contains a syntax error in the switch statement?
Aswitch ($value) { 1 { 'One' } 2 { 'Two' } default { 'Other' } }
B} } 'rehtO' { tluafed } 'owT' { 2 } 'enO' { 1 { )eulav$( hctiws
Cswitch ($value) { 1: { 'One' } 2: { 'Two' } default: { 'Other' } }
Dswitch $value { 1 { 'One' } 2 { 'Two' } default { 'Other' } }
Attempts:
2 left
💡 Hint
Check the use of colons vs braces in switch cases.
🚀 Application
advanced
2:00remaining
Using switch to process multiple inputs
What is the output of this script?
PowerShell
switch (1, 2, 3) {
  1 { 'One' }
  2 { 'Two' }
  3 { 'Three' }
  default { 'Other' }
}
A
One
Two
Three
BOne
COther
D
One
Other
Attempts:
2 left
💡 Hint
Switch processes each input value separately.
🧠 Conceptual
expert
2:00remaining
Behavior of switch with -Wildcard parameter
Given this script, what is the output?
PowerShell
switch -Wildcard ("apple.txt", "banana.doc", "cherry.txt") {
  '*.txt' { "Text file: $_" }
  '*.doc' { "Doc file: $_" }
  default { "Other file: $_" }
}
A
Text file: apple.txt
Doc file: banana.doc
Text file: cherry.txt
B
Other file: apple.txt
Other file: banana.doc
Other file: cherry.txt
C
Text file: apple.txt
Text file: banana.doc
Text file: cherry.txt
D
Doc file: apple.txt
Doc file: banana.doc
Doc file: cherry.txt
Attempts:
2 left
💡 Hint
The -Wildcard parameter enables wildcard matching on input values.