📖 Scenario: You are working with a list of temperatures recorded during a day. You want to find out which temperatures are above a certain threshold and also check if the temperature is exactly zero. This helps you understand how comparison and logical operators work in PowerShell.
🎯 Goal: Build a simple PowerShell script that uses comparison and logical operators to filter temperatures and check conditions.
📋 What You'll Learn
Create a list of temperatures as integers
Create a threshold variable for comparison
Use comparison operators to find temperatures above the threshold
Use logical operators to check if any temperature is zero
Print the filtered temperatures and the zero check result
💡 Why This Matters
🌍 Real World
Filtering and checking data values is common in scripts that monitor sensors, logs, or user inputs.
💼 Career
Understanding comparison and logical operators is essential for writing scripts that make decisions based on data.
Progress0 / 4 steps
1
Create the temperature list
Create a variable called temperatures and assign it the list of integers 5, -3, 0, 12, 7.
PowerShell
Hint
Use a comma-separated list of numbers assigned to $temperatures.
2
Set the threshold value
Create a variable called threshold and set it to 4.
PowerShell
Hint
Assign the number 4 to the variable $threshold.
3
Filter temperatures above threshold and check for zero
Create a variable called aboveThreshold that contains all temperatures from $temperatures greater than $threshold using the comparison operator -gt. Then create a variable called hasZero that is $true if any temperature equals zero using the comparison operator -eq inside a loop.
PowerShell
Hint
Use Where-Object with -gt to filter. Use a foreach loop and -eq to check zero.
4
Print the results
Print the variable aboveThreshold and then print the text Zero found: $hasZero.
PowerShell
Hint
Simply output $aboveThreshold and then use Write-Output to print the zero check.
Practice
(1/5)
1. In PowerShell, which operator is used to check if two values are equal?
easy
A. -gt
B. -ne
C. -eq
D. -lt
Solution
Step 1: Understand comparison operators
PowerShell uses specific operators to compare values, such as -eq for equality, -ne for not equal, -gt for greater than, and -lt for less than.
Step 2: Identify the equality operator
The operator -eq checks if two values are equal, returning True if they are the same.
Final Answer:
-eq -> Option C
Quick Check:
Equality operator = -eq [OK]
Hint: Remember: 'eq' means equal in PowerShell [OK]
Common Mistakes:
Confusing -eq with -ne (not equal)
Using == instead of -eq
Mixing up -gt and -lt
2. Which of the following is the correct syntax to check if variable $a is greater than 10 in PowerShell?
easy
A. if ($a > 10) { }
B. if ($a -gt 10) { }
C. if ($a gt 10) { }
D. if ($a => 10) { }
Solution
Step 1: Recall PowerShell comparison syntax
PowerShell uses operators with a dash, like -gt for greater than, not symbols like > or =>.
Step 2: Identify the correct operator for greater than
The correct operator is -gt, so the syntax is if ($a -gt 10) { }.
Final Answer:
if ($a -gt 10) { } -> Option B
Quick Check:
Greater than operator = -gt [OK]
Hint: Use dash before comparison operators in PowerShell [OK]
Common Mistakes:
Using > instead of -gt
Writing 'gt' without dash
Using invalid operator =>
3. What will be the output of this PowerShell code?