PowerShell equality operators are used to compare values. These operators compare the values and return either
False; when the given condition matches.
PowerShell provides below equality operators; as these are used for comparison, we can call them Comparison operators.
PowerShell Equality operators
All these equality operators takes 2 or more operands and compare the values; produce the comparison results. Results? Yes. These operators compares the values and returns
False as the comparison result, if these applied on 2 operands. If more than 2 inputs are given, these operators returns the results with satisfied values. Let’s look at these with examples, below;
These operators verifies the equality check; whether the values of the given operands are equal (
-eq) or not equal (
-ne). These returns,
True, if the comparison satisfies; otherwise, these returns the value
We can use these operators with numbers, arrays or collections and strings. When we use these operators with numbers; these simply compares the numbers and returns the result,
PS C:\> 1 -eq 1 True PS C:\> 1 -ne 2 True PS C:\> 12 -eq 20 False
When we use these operators with strings; these will compare the strings and returns the value
False. The comparison is non-case sensitive; that means; these treats “Apple” and “apple” as the same strings. Capitals & Smalls, treats as equal;
PS C:\> "Buffer" -eq "buffer" True PS C:\> "True" -ne "true" False
These operators allows; multiple values in left side and a single value in right side of the operator. If multiple values are given, each value must be separated by a comma (“,”). These will compare the right side value with the values in left side; and returns the matching entries. We can combine string & numbers when we use multiple values in the comparison;
PS C:\> 1, 2, “Hi!” -eq 1
PS C:\> (1, 2, "Hi!") -ne 1 2 Hi!
Greater than (
-gt) and greater than or equal to (
-ge) operators are used to compare the operands to identify which operand’s value is greater than or greater than or equal to, other operand’s value. These also takes 2 or more operands as inputs. Multiple operands must be separated by a comma (“,”); and these can be used only left side of the operator. Right side, always use the single operand. Otherwise, PowerShell throws an Error;
PS C:\> 1, 2 -gt 3, 4 Could not compare "1" to "3 4". Error: "Cannot convert the "System.Object" value of type "System.Object" to type "System.Int32"." At line:1 char:1 + 1, 2 -gt 3, 4 + ~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) , RuntimeException + FullyQualifiedErrorId : ComparisonFailure
We can use these operators with numbers & strings. String comparison is non-case sensitive. Here are some examples;
PS C:\> 1 -gt 2 False PS C:\> 1, 2, 3 -gt 2 3 PS C:\> "hello!" -ge "hi!" False PS C:\> "PowerShell" -ge "powershell" True PS C:\> 10, "code" -gt 2 10 code
Observe that, we can combine numbers & strings in the comparison. Also, multiple values are separated by commas (“,”). When we use multiple values; the values which satisfies the condition returns as the result.
And we can not compare a number with a string; if we attempt, PowerShell will show the below Error;
PS C:\> 345 -gt "Hello, World!" Cannot convert value "hello, World!" to type "System.Int32". Error: "Input string was not in a correct format." At line:1 char:1 + 345 -gt "hello, World!" + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) , RuntimeException + FullyQualifiedErrorId : InvalidCastFromStringToInteger
Less than (
-lt) and less than or equals to (
-le) operators works similar to the above operators; and these will compare for the lesser value than the other value. Here are some examples;
PS C:\> 1, 2, 3 -lt 2 1 PS C:\> 0 -le 9 True PS C:\> "False" -le "FALSE" True
With these operators, we can not compare a number with a string; same as above.
These are the equality operators PowerShell provides for equality check.