PowerShell Comparison Operators
A comparison operator is a symbol that tests two values and gives back True or False. You use comparison operators to ask questions about numbers and text, such as whether one value is bigger than another.
Compare numbers
Put the operator between two numbers. The result is True or False.
$score = 85
$score -gt 80
$score -lt 90
$score -eq 85
$score -ne 100Compare text without caring about case
-eq ignores the case of letters. -ceq is the case-sensitive form, and it cares about case.
$name = "Ana"
$name -eq "ana"
$name -ceq "ana"
"apple" -eq "Apple"
"apple" -ceq "Apple"Test a boundary value
-ge and -le include the boundary value itself. A score of 25 passes a test for 25 or more.
$price = 25
$price -ge 25
$price -le 25
$price -gt 25
$price -lt 25Example
$name = "Ana"
$score = 78
"Name matches:"
$name -eq "ana"
"Score passes:"
$score -ge 70
"Score below 80:"
$score -lt 80
"Score is not 100:"
$score -ne 100Key points
- Comparison operator: a symbol that compares two values and returns True or False.
- -eq tests whether two values are equal.
- -ne tests whether two values are not equal.
- -gt tests whether the left value is greater than the right value.
- -ge tests whether the left value is greater than or equal to the right value.
- -lt and -le test whether the left value is less than, or less than or equal to, the right value.
In this example, a name and a score are stored in variables. The name test uses -eq, so it ignores letter case and returns True. The score tests check a passing mark, an upper limit, and a value the score is not, and each one prints True or False.
Note
-eq ignores letter case, so "apple" equals "Apple", while -ceq cares about case and says they differ.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Which of these comparisons returns True?