0
0
PowerShellscripting~15 mins

If-elseif-else statements in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
If-elseif-else statements
📖 Scenario: You are creating a simple script to check the temperature and give advice on what to wear. This is like asking a friend, "Is it cold, warm, or hot outside?" and then deciding what clothes to wear based on that.
🎯 Goal: Build a PowerShell script that uses if, elseif, and else statements to check a temperature value and print the right clothing advice.
📋 What You'll Learn
Create a variable called temperature with a specific number value.
Create a variable called coldThreshold with a number value.
Use if, elseif, and else statements to compare temperature with coldThreshold and another threshold.
Print the correct advice message based on the temperature.
💡 Why This Matters
🌍 Real World
Scripts like this help automate decisions based on data, such as adjusting settings or sending alerts depending on conditions.
💼 Career
Understanding conditional statements is essential for scripting tasks in IT, system administration, and automation roles.
Progress0 / 4 steps
1
Set the temperature value
Create a variable called temperature and set it to 15.
PowerShell
Need a hint?

Use $temperature = 15 to create the variable.

2
Set the cold temperature threshold
Create a variable called coldThreshold and set it to 10.
PowerShell
Need a hint?

Use $coldThreshold = 10 to create the variable.

3
Write the if-elseif-else statements
Write an if statement to check if temperature is less than coldThreshold. Then add an elseif to check if temperature is less than 25. Use else for all other cases. Inside each block, assign a string message to a variable called advice as follows:
- If cold: "Wear a jacket"
- Elseif warm: "Wear a t-shirt"
- Else: "Wear shorts"
PowerShell
Need a hint?

Use if ($temperature -lt $coldThreshold) for the cold check, then elseif ($temperature -lt 25) for warm, and else for hot.

4
Print the clothing advice
Print the value of the advice variable using Write-Output.
PowerShell
Need a hint?

Use Write-Output $advice to print the message.