Comparison operators help you check if values are equal, different, or if one is bigger or smaller than another. This is useful to make decisions in scripts.
Check if a user input matches a password.
Compare numbers to see if a score is higher than a target.
Find out if two strings are the same.
Decide if a file size is larger than a limit.
Run different commands based on a condition.
Syntax
PowerShell
value1 -eq value2 # equals
value1 -ne value2 # not equals
value1 -gt value2 # greater than
value1 -lt value2 # less than
Use -eq to test if two values are equal.
Use -ne to test if two values are not equal.
Examples
This checks if 5 is equal to 5. It returns $True.
PowerShell
5 -eq 5
This checks if the word "apple" is not equal to "orange". It returns $True.
PowerShell
"apple" -ne "orange"
This checks if 10 is greater than 7. It returns $True.
PowerShell
10 -gt 7
This checks if 3 is less than 2. It returns $False.
PowerShell
3 -lt 2
Sample Program
This script prints the result of four comparisons using the operators. It shows $True or $False for each.
PowerShell supports elseif as one word; braces are balanced.
Step 2: Verify operators and structure
-gt and -lt are correct operators; no syntax errors present.
Final Answer:
No error, code runs correctly -> Option B
Quick Check:
elseif is valid in PowerShell [OK]
Hint: PowerShell uses elseif as one word, not else if [OK]
Common Mistakes:
Thinking elseif is invalid
Expecting > instead of -gt
Missing braces confusion
5. You want to write a PowerShell script that prints 'Valid' only if a variable $score is greater than or equal to 70. Which code snippet correctly uses comparison operators to achieve this?
hard
A. if ($score -gt 70) { 'Valid' } else { 'Invalid' }
B. if ($score -lt 70) { 'Valid' } else { 'Invalid' }
C. if ($score -eq 70) { 'Valid' } else { 'Invalid' }
D. if ($score -ge 70) { 'Valid' } else { 'Invalid' }
Solution
Step 1: Understand the requirement
We want to check if $score is greater than or equal to 70.
Step 2: Identify the correct operator
-ge means greater than or equal to, which matches the requirement.
Final Answer:
if ($score -ge 70) { 'Valid' } else { 'Invalid' } -> Option D
Quick Check:
Greater or equal operator = -ge [OK]
Hint: Use -ge for greater or equal comparisons [OK]
Common Mistakes:
Using -gt excludes 70
Using -eq only matches exactly 70
Using -lt reverses logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.