Monday, May 8, 2023
HomeSoftware TestingHow To Use Home windows PowerShell Not Equal Operator

How To Use Home windows PowerShell Not Equal Operator


When two objects’ values will not be equal, the PowerShell Not Equal operator (-ne) checks for this after which produces a Boolean worth. The result’s True if the values will not be similar; else, the result’s False.

Methods to Use Home windows PowerShell Not Equal Operator (comparability operator)? 

1. Evaluating Variables

Evaluating two variables is among the most frequent makes use of of the Home windows PowerShell Not Equal operator. Let’s say you wish to examine to see if two variables that you’ve got are equal. The next is the comparability syntax:

$a -ne $b
  • This comparability will return True if the values of $a and $b will not be equal. If not, False will probably be returned.
  • Let every of the variables $a, $b, $c, $d, and $e symbolize a separate worth. To see if they’re equal, you may evaluate every of those values.
$a, $b, $c, $d, and $e represent a separate value
  • To find out whether or not the values of $a and $b are equal or not, use the PowerShell Not Equal comparability.
$a -ne $b
  • As a result of $a has a price of 5 and $b of 6, which aren’t equal, the result’s True.
  • Now let’s consider variables with disparate values.
image
How To Use Home windows PowerShell Not Equal Operator | 4 Greatest Makes use of 11
  • The values of $b and $d are equal, therefore you will notice a False output end result. 

2. Evaluating Values

Not all of the belongings you want to evaluate whereas utilizing PowerShell are in variables. The PowerShell Not Equal operator can be utilized to immediately evaluate values on this state of affairs.

  • As an illustration, the script that follows makes use of the PowerShell Not Equal operator to explicitly evaluate two numbers.
5 -ne 3
  • Because the values aren’t equal, you’ll get a True worth return, as demonstrated under.
  • Evaluating equal values, however, will end in False.
5 -ne 5

3. Eradicating a Worth from the Array

To be able to decide whether or not two values will not be equal, the PowerShell Not Equal operator supplies a Boolean end result. Along with that functionality, this operator can exclude sure entries from an array.

Create a variable referred to as $numbers, as an example, and fill it with an array of the digits 1 by way of 9.

Create an array with the digits 1 by way of 9 utilizing the syntax 

image 21

Execute the command listed under to get the array again except for quantity 6.

$numbers = 1..9

$numbers
How To Use Home windows PowerShell Not Equal Operator | 4 Greatest Makes use of 12

Now you can see that, except for quantity 6, each quantity within the array was returned by the command.

4. Evaluating Strings

You need to use the PowerShell Not Equal comparability on strings along with numbers.

Take the strings “Whats up” and “World” for example. Run the command under to see if the 2 strings are equal.

"Whats up" -ne "World"
"Hello" -ne "World"

As “Whats up” shouldn’t be similar to “World,” the conclusion is True.

Now examine to confirm if the strings “Whats up” and “good day” are equal.

"Whats up" -ne "good day"
"Hello" -ne "hello"

The end result will subsequently be False since “Whats up” is the same as “good day,” as you may see.

There isn’t any case-sensitive with the PowerShell Not Equal operator. The previous step confirmed that “Hey” is equal to “good day.”

When evaluating string values, exchange the -ne operator with the -cne operator, which stands for Case Delicate Not Equal, if case sensitivity is critical.

"Whats up" -cne "good day"

PowerShell now not considers “Whats up” and “good day” to be equal and the operator returns false.

"Hello" -cne "hello"

Some Helpful Mathematical Operators in Powershell

1. Arithmetic Operators

  • + (Addition): The operator + (Addition) provides values to each side of it.
  • – (Subtraction): Removes the right-hand operand from the left-hand operand (subtraction).
  • * (Multiplication): Values on both facet of the operator are multiplied by the * operator.
  • / (Division): Divides the left-hand operand by the right-hand operand utilizing the image /.
  • % (Modulus): Returns the rest after dividing the left-hand operand by the right-hand operand.

2. Comparability Operators

  • equals (-eq). evaluates whether or not two values are equal.
  • not equal, or -ne. checks for inequality between two values.
  • better than, or -gt. compares the primary and second values to see which is larger.
  • better than or equal to, or -ge. checks whether or not the primary quantity is bigger than or equal to the second.
  • lower than, or -lt. demonstrates that the primary worth is lower than the second.
  • (Lower than or equal to) -le. compares the primary worth to see whether it is decrease than or equal to the second.

3. Logical Operators

  • AND (logical and) Referred to as Logical AND operator. The situation turns into true if neither of the operands is zero.
  • Referred to as Logical OR Operator. The situation is met if both of the 2 operands is non-zero.
  • NOT (logical not) Referred to as Logical NOT Operator.  The logical NOT operator will render a situation unfaithful whether it is true.

4. Task Operators

  • = Primary project operator. values from the correct facet operands are assigned to the left facet operand.
  • Task operator += Add AND.  The precise operand is added to the left operand, and the result’s assigned to the left operand.
  • -= The project operator for subtraction AND. The precise operand is subtracted from the left operand, and the result’s assigned to the left operand.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments