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.
0
0
Comparison operators (-eq, -ne, -gt, -lt) in PowerShell
Introduction
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
Write-Output (5 -eq 5) Write-Output (10 -ne 10) Write-Output (7 -gt 3) Write-Output (2 -lt 4)
OutputSuccess
Important Notes
Comparison operators return $True or $False, which are Boolean values.
Strings are compared by their text content, not by case sensitivity unless specified.
Use parentheses to make sure comparisons are evaluated correctly in complex expressions.
Summary
Comparison operators help you compare values easily in PowerShell.
Use -eq, -ne, -gt, and -lt for equals, not equals, greater than, and less than.
They return $True or $False to help your script decide what to do next.