Bird
Raised Fist0
PowerShellscripting~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which PowerShell operator checks if two values are equal?
easy
A. -eq
B. -ne
C. -gt
D. -lt

Solution

  1. Step 1: Understand the meaning of each operator

    -eq means equals, -ne means not equals, -gt means greater than, -lt means less than.
  2. Step 2: Identify the operator for equality

    The operator that checks if two values are equal is -eq.
  3. Final Answer:

    -eq -> Option A
  4. Quick Check:

    Equals operator = -eq [OK]
Hint: Remember: eq means equal, ne means not equal [OK]
Common Mistakes:
  • Confusing -eq with -ne
  • Mixing -gt and -lt
  • Using == instead of -eq
2. Which of the following is the correct syntax to check if variable $a is greater than 10 in PowerShell?
easy
A. if ($a -gt 10) { }
B. if ($a > 10) { }
C. if ($a -lt 10) { }
D. if ($a -eq 10) { }

Solution

  1. Step 1: Recall PowerShell comparison syntax

    PowerShell uses -gt for greater than, not > like some other languages.
  2. Step 2: Match the correct operator

    The correct syntax to check if $a is greater than 10 is if ($a -gt 10) { }.
  3. Final Answer:

    if ($a -gt 10) { } -> Option A
  4. Quick Check:

    Greater than operator = -gt [OK]
Hint: Use -gt for greater than, not > in PowerShell [OK]
Common Mistakes:
  • Using > instead of -gt
  • Confusing -lt with -gt
  • Missing parentheses in if statement
3. What will be the output of this PowerShell code?
$x = 5
if ($x -ne 5) { 'No' } else { 'Yes' }
medium
A. 5
B. No
C. Yes
D. False

Solution

  1. Step 1: Evaluate the condition $x -ne 5

    $x is 5, so $x -ne 5 means 5 not equal to 5, which is False.
  2. Step 2: Determine which block runs

    Since condition is False, the else block runs, outputting 'Yes'.
  3. Final Answer:

    Yes -> Option C
  4. Quick Check:

    5 not equal 5 is False, so else runs [OK]
Hint: -ne means not equal; false triggers else block [OK]
Common Mistakes:
  • Thinking -ne means equal
  • Confusing output with variable value
  • Ignoring else block
4. Identify the error in this PowerShell snippet:
$num = 10
if ($num -gt 5) { 'Greater' } elseif ($num -lt 5) { 'Smaller' } else { 'Equal' }
medium
A. Missing closing brace for if block
B. No error, code runs correctly
C. Incorrect use of elseif instead of else if
D. Using -gt instead of > operator

Solution

  1. Step 1: Check syntax of if-elseif-else

    PowerShell supports elseif as one word; braces are balanced.
  2. Step 2: Verify operators and structure

    -gt and -lt are correct operators; no syntax errors present.
  3. Final Answer:

    No error, code runs correctly -> Option B
  4. 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

  1. Step 1: Understand the requirement

    We want to check if $score is greater than or equal to 70.
  2. Step 2: Identify the correct operator

    -ge means greater than or equal to, which matches the requirement.
  3. Final Answer:

    if ($score -ge 70) { 'Valid' } else { 'Invalid' } -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard