0
0
PowerShellscripting~10 mins

Comparison operators (-eq, -ne, -gt, -lt) in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Comparison operators (-eq, -ne, -gt, -lt)
Start
Evaluate Left Value
Evaluate Right Value
Apply Comparison Operator
Result: True or False
Use Result in Script
End
The script compares two values using a comparison operator and produces True or False as the result.
Execution Sample
PowerShell
$a = 5
$b = 3
$a -gt $b
$a -eq $b
$a -ne $b
This code compares two numbers using greater than, equal, and not equal operators.
Execution Table
StepExpressionLeft ValueRight ValueOperatorResultExplanation
1$a -gt $b53-gtTrue5 is greater than 3
2$a -eq $b53-eqFalse5 is not equal to 3
3$a -ne $b53-neTrue5 is not equal to 3
4EndAll comparisons done
💡 All comparisons evaluated, script ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$a55555
$b33333
Key Moments - 2 Insights
Why does $a -gt $b return True but $a -eq $b return False?
Because 5 is greater than 3, so -gt returns True. But 5 is not equal to 3, so -eq returns False. See execution_table rows 1 and 2.
What does -ne operator do?
It checks if two values are not equal. If they differ, it returns True. See execution_table row 3 where 5 -ne 3 is True.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of $a -eq $b at step 2?
ATrue
B5
CFalse
D3
💡 Hint
Check the 'Result' column in row 2 of the execution_table.
At which step does the comparison check if $a is greater than $b?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Operator' column to find '-gt' in the execution_table.
If $b was changed to 5, what would be the result of $a -eq $b at step 2?
AFalse
BTrue
CError
DNull
💡 Hint
If both values are equal, -eq returns True. Refer to the meaning of -eq in key_moments.
Concept Snapshot
Comparison operators in PowerShell:
-eq : equals (True if values are same)
-ne : not equals (True if values differ)
-gt : greater than (True if left > right)
-lt : less than (True if left < right)
Used to compare values and return True or False.
Full Transcript
This lesson shows how PowerShell compares two values using operators like -eq, -ne, -gt, and -lt. We start by assigning 5 to $a and 3 to $b. Then we check if $a is greater than $b using -gt, which returns True because 5 is greater than 3. Next, we check if $a equals $b with -eq, which returns False because 5 is not equal to 3. Finally, we check if $a is not equal to $b with -ne, which returns True because they differ. These operators help scripts make decisions by comparing values and returning True or False.