PowerShell provides different comparison operators to compare different values, objects, variables, outputs, etc. The Not Equal
operator is used to check if the specified two-parameters are equal or not. If two specified parameters are not the same the not equal operator returns True
boolean value. If two specified parameters are the same the not equal operator returns False
boolean value.
Not Equal Operator Syntax
The not equal operator is expressed with the -ne
the first letters of the “not equal”. The syntax of the not equal operator is like below. The not equal operator returns a boolean value True or False according to the result.
A -ne B
- A can be a variable, value or object which compared to B for inequailty.
- B can be a variable, value or object which compared to A for inequailty.
Check If Variables Not Equal
Variables can be compared if they are equal or not using the not equal operator.
$a = 2
$b = 2
$c = 3
$result = $a -ne $b
# result is False
$result = $a -ne $c
#result is True
Check If Values Not Equal
The not equal operator can be also used with the values without using variables.
$result = "a" -ne "b"
#result is True
$result = 2 -ne 3
#result is True
Check If Strings Not Equal
String variables and values can be checked for inequality by using the not equal operator.
$a="ismail"
$b = "ahmet"
$result = $a -ne $b
#result is True
$result = "ismail" -ne $b
#result is True
$result = "ismail" -ne "ahmet"
#result is True
$result = $a -ne "ismail"
#result is False
Check If Numbers Not Equal
One of the most popular usages for the not equal operator is to check numbers for their equality.
$a = 1
$b = 2
$result = $a -ne $b
#result is True
$result = $a -ne 3
#result is True
$result = 3 -ne 3
#result is False
Check Collection/Multiple Values If Not Equal
The not equal operator can be used to check multiple values or a collection for inequality. In the following example, we check if variables a, b and c are not equal to 3 and return a collection that is not equal to the 3.
$a = 1
$b = 2
$c = 3
$result = $a,$b,$c -ne 3
$result is a collection which consist of 1 and 2 as they are not equal to the 3