0
0
PowerShellscripting~15 mins

Switch statement in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Switch Statement in PowerShell
📖 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
Need a 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.
PowerShell
Need a hint?

Use switch ($fruitColor) { "red" { } "yellow" { } "green" { } } structure.

3
Add actions inside switch cases
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
Need a 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
Need a hint?

Run the script and check the output matches exactly.