How To Use Windows PowerShell Not Equal Operator | 4 Best Uses

When two objects’ values are not equal, the PowerShell Not Equal operator (-ne) checks for this and then produces a Boolean value. The result is True if the values are not same; else, the result is False.

Ways to Use Windows PowerShell Not Equal Operator (comparison operator)? 

1. Comparing Variables

Comparing two variables is one of the most frequent uses of the Windows PowerShell Not Equal operator. Let’s say you want to check to see if two variables that you have are equal. The following is the comparison syntax:

$a -ne $b
  • This comparison will return True if the values of $a and $b are not equal. If not, False will be returned.
  • Let each of the variables $a, $b, $c, $d, and $e represent a separate value. To see if they are equal, you can compare each of these values.
$a, $b, $c, $d, and $e represent a separate value
  • To determine whether the values of $a and $b are equal or not, use the PowerShell Not Equal comparison.
    •  $a -ne $b
$a -ne $b
  • Because $a has a value of 5 and $b of 6, which are not equal, the result is True.
  • Now let’s evaluate variables with disparate values.
    • $b -ne $d
image
  • The values of $b and $d are equal, hence you will see a False output result. 

2. Comparing Values

Not all the things you wish to compare while using PowerShell are in variables. The PowerShell Not Equal operator can be used to directly compare values in this situation.

5 -ne 3
  • Since the values aren’t equal, you’ll get a True value return, as demonstrated below.
  • Comparing equal values, on the other hand, will result in False.
    • 5 -ne 5
5 -ne 5

3. Removing a Value from the Array

In order to determine whether two values are not equal, the PowerShell Not Equal operator provides a Boolean result. In addition to that capability, this operator can exclude certain entries from an array.

Create a variable called $numbers, for instance, and fill it with an array of the digits 1 through 9.

Create an array with the digits 1 through 9 using the syntax 

  • $numbers = 1..9
  • $numbers
image 21

Execute the command listed below to get the array back with the exception of number 6.

  • $numbers -ne 6
$numbers = 1..9

$numbers

You can now see that, with the exception of number 6, every number in the array was returned by the command.

4. Comparing Strings

You can use the PowerShell Not Equal comparison on strings in addition to numbers.

Take the strings “Hello” and “World” as an example. Run the command below to see if the two strings are equal.

"Hello" -ne "World"
"Hello" -ne "World"

As “Hello” is not identical to “World,” the conclusion is True.

Now check to verify if the strings “Hello” and “hello” are equal.

"Hello" -ne "hello"
"Hello" -ne "hello"

The outcome will therefore be False since “Hello” is equal to “hello,” as you can see.

There is no case-sensitive with the PowerShell Not Equal operator. The preceding step showed that “Hey” is equivalent to “hello.”

When comparing string values, replace the -ne operator with the -cne operator, which stands for Case Sensitive Not Equal, if case sensitivity is necessary.

"Hello" -cne "hello"

PowerShell no longer considers “Hello” and “hello” to be equivalent and the operator returns false.

"Hello" -cne "hello"

Some Useful Mathematical Operators in Powershell

1. Arithmetic Operators

  • + (Addition): The operator + (Addition) adds values to both sides of it.
  • – (Subtraction): Removes the right-hand operand from the left-hand operand (subtraction).
  • * (Multiplication): Values on either side of the operator are multiplied by the * operator.
  • / (Division): Divides the left-hand operand by the right-hand operand using the symbol /.
  • % (Modulus): Returns the remainder after dividing the left-hand operand by the right-hand operand.

2. Comparison Operators

  • equals (-eq). evaluates whether two values are equivalent.
  • not equal, or -ne. checks for inequality between two values.
  • greater than, or -gt. compares the first and second values to see which is greater.
  • greater than or equal to, or -ge. checks whether the first number is larger than or equal to the second.
  • less than, or -lt. demonstrates that the first value is less than the second.
  • (Less than or equal to) -le. compares the first value to see if it is lower than or equal to the second.

3. Logical Operators

  • AND (logical and) Called Logical AND operator. The condition becomes true if neither of the operands is zero.
  • Called Logical OR Operator. The condition is met if either of the two operands is non-zero.
  • NOT (logical not) Called Logical NOT Operator.  The logical NOT operator will render a condition untrue if it is true.

4. Assignment Operators

  • = Basic assignment operator. values from the right side operands are assigned to the left side operand.
  • Assignment operator += Add AND.  The right operand is added to the left operand, and the result is assigned to the left operand.
  • -= The assignment operator for subtraction AND. The right operand is subtracted from the left operand, and the result is assigned to the left operand.
Meet the Author

Abdul Rahim has been working in Information Technology for over two decades. Learn how Abdul got his start as a Tech Blogger , and why he decided to start this Software blog. If you want to send Abdul a quick message, then visit his contact page here.