0
0
PowerShellscripting~15 mins

Ternary operator (PowerShell 7+) - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the Ternary Operator in PowerShell 7+
📖 Scenario: You are working on a simple script to check if a number is even or odd. This is like checking if a number is divisible by 2 without any remainder, which is something you might do when sorting items into two groups.
🎯 Goal: Build a PowerShell script that uses the ternary operator to decide if a number is even or odd and then prints the result.
📋 What You'll Learn
Create a variable called number with the value 7
Create a variable called result that uses the ternary operator to check if number is even or odd
Print the result variable
💡 Why This Matters
🌍 Real World
Checking conditions quickly and assigning values based on those conditions is common in scripts that automate tasks, like sorting files or deciding what action to take.
💼 Career
Understanding the ternary operator helps you write concise and readable scripts, which is valuable for system administrators and automation engineers.
Progress0 / 4 steps
1
Create the number variable
Create a variable called number and set it to 7.
PowerShell
Need a hint?

Use = to assign the value 7 to the variable number.

2
Add the ternary operator to check even or odd
Create a variable called result that uses the ternary operator to check if number % 2 equals 0. If yes, set result to 'Even', otherwise set it to 'Odd'.
PowerShell
Need a hint?

Use $number % 2 -eq 0 ? 'Even' : 'Odd' syntax for the ternary operator in PowerShell 7+.

3
Print the result
Write a line to print the value of the result variable.
PowerShell
Need a hint?

Use Write-Output to display the value of $result.

4
Test with a different number
Change the value of number to 10 and print the result again using the ternary operator.
PowerShell
Need a hint?

Assign 10 to number, then use the ternary operator again to update result, and print it.