📖 Scenario: You are creating a simple script to categorize fruits based on their color. This will help a fruit seller quickly identify the type of fruit by its color.
🎯 Goal: Build a PowerShell script that uses a switch statement to print the fruit category based on the fruit color.
📋 What You'll Learn
Create a variable with a fruit color
Create a variable with a fruit name
Use a switch statement to check the fruit color
Print the fruit category based on the color
Handle at least three colors: red, yellow, green
💡 Why This Matters
🌍 Real World
Switch statements help automate decisions based on different input values, like categorizing items quickly.
💼 Career
Knowing switch statements is useful for scripting tasks in IT, system administration, and automation roles.
Progress0 / 4 steps
1
Create fruit color and fruit name variables
Create a variable called $fruitColor and set it to "red". Create another variable called $fruitName and set it to "Apple".
PowerShell
Hint
Use = to assign values to variables. Strings must be in double quotes.
2
Add a switch statement for fruit color
Add a switch statement that checks the value of $fruitColor. Prepare cases for "red", "yellow", and "green". For now, just add the switch block without any actions inside.
Inside the switch statement, add Write-Output commands for each color case. For "red", print "$fruitName is a red fruit". For "yellow", print "$fruitName is a yellow fruit". For "green", print "$fruitName is a green fruit".
PowerShell
Hint
Use Write-Output with double quotes and $fruitName inside the string.
4
Print the fruit category
Run the script to print the fruit category based on the $fruitColor. Use Write-Output inside the switch statement as implemented. The output should be exactly: Apple is a red fruit.
PowerShell
Hint
Run the script and check the output matches exactly.
Practice
(1/5)
1. What is the main purpose of a switch statement in PowerShell?
easy
A. To create a variable
B. To repeat a block of code multiple times
C. To define a function
D. To choose one action from many based on a value
Solution
Step 1: Understand the role of switch
A switch statement checks a value and picks the matching case to run.
Step 2: Compare with other options
Loops repeat code, functions define reusable code, variables store data. Only switch picks actions based on value.
Final Answer:
To choose one action from many based on a value -> Option D
Quick Check:
Switch selects action by value = A [OK]
Hint: Switch picks code by matching value, like a menu choice [OK]
Common Mistakes:
Confusing switch with loops
Thinking switch defines variables
Mixing switch with function definition
2. Which of the following is the correct syntax to start a switch statement in PowerShell?
easy
A. switch ($variable) { ... }
B. switch $variable { ... }
C. switch { $variable } ...
D. switch ($variable) then { ... }
Solution
Step 1: Recall switch syntax
PowerShell requires parentheses around the value: switch ($variable) { ... }
Step 2: Check other options
switch $variable { ... } misses parentheses, C has wrong braces, D uses invalid 'then'.
Final Answer:
switch ($variable) { ... } -> Option A
Quick Check:
Parentheses around variable = B [OK]
Hint: Always put the variable inside parentheses after switch [OK]
Common Mistakes:
Omitting parentheses around the variable
Using 'then' keyword incorrectly
Wrong brace placement
3. What will be the output of this PowerShell code?
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 ',').