powershell

What are Powershell Logical Operators ? — Top Tech Notes

Manish Chandra
6 min readJan 26, 2022

--

Logical operators are used to make decision based on multiple conditions. And in a script logical operators are used to control the program flow. So to write a better and logical scripts you need to understand the use of logical operators.

Logical operators

Logical operators allows us to connect different statements or expressions together and thus allows us to use a single expression to test for multiple conditions. The logical operators are very important because they allows us to create filters of the data that we retrieve from systems and also allows us to create our own custom checks of conditions. There are four main logical operators in powershell. They are :

  • The Logical AND operator (-and)
  • The logical OR operator (-or)
  • The logical exclusive OR operator (-xor)
  • And the logical not operator (-not)

The logical not operator is also represented by an exclamatory sign “!”.

Let us understand about each of them with some examples.

-and operator

The logical AND operator returns TRUE when both statements are correct. If there are more statement and only -and operator is used, then all statements should be correct. Let’s take an example of this operator

If we write in powershell:

(5 -eq 5) -and (6 -eq 6)

Then it returns TRUE because both conditions are satisfied. And if we write:

(5 -eq 5) -and (6 -eq 7)

It returns FALSE because first condition is TRUE but second condition is not satisfied. 6 is not equal to 7. So FALSE is returned. Again if we write:

(5 -eq 6) -and (6 -eq 6)

Then again it returns false because second condition is satisfied but first condition is not satisfied. Similarly if we write:

(5 -eq 6) -and (6 -eq 7)

powershell

Then also it returns FALSE because none of the two conditions is satisfied.

-OR operator

The -or operator will return true if at least one of the statements are correct. It does not matter how many statements you are using. If only one them is true then all the expression is true.

Let’s use -or operator in the 4 expressions that we have used with AND operator.

If we write:

(5 -eq 5) -or (6 -eq 6)

Then powershell return TRUE because both the conditions are true.

Again if we write in powershell:

(5 -eq 5) -or (6 -eq 7)

Then also powershell returns TRUE because the first condition is true. Though second condition is not satisfied but OR operator needs only one condition to be satisfied to return TRUE.

Again if we write in powershell:

(5 -eq 6) -or (6 -eq 6)

Now also powershell returns TRUE because the second condition is satisfied.

And if we write:

(5 -eq 6) -or (6 -eq 7)

Then powershell returns FALSE because none of the condition is satisfied.

-XOR operator

The -xor operator returns true if only one statement in an expression is true. It is called exclusive or. It does not matter how many statements you are using in an expression. If more than one statement is true then the expression will return false.

So if we write in powershell:

(5 -eq 5) -xor (6 -eq 6)

It returns FALSE because both the conditions are satisfied while exclusive OR operator returns TRUE only when one condition is satisfied.

Again if we write in powershell:

(5 -eq 5) -xor (6 -eq 7)

Then it returns TRUE because only first condition is satisfied.

Again if we write in powershell:

(5 -eq 6) -xor (6 -eq 6)

Powershell returns TRUE because only second condition is satisfied. Similarly if we write in powershell:

(5 -eq 6) -xor (6 -eq 7)

Then powershell returns FALSE because none of the condition is satisfied.

-Not operator

Let’s now talk about the -Not operator.

The -not operator will turn the result of a statement to the opposite. If the return value is true for a statement then it will return false and the opposite.

This -not operator is also represented by the symbol “!”.

So whether we write -not or ! in our expression, both will give the same result. Let’s take some examples of this operator.

If we write in powershell:

(5 -eq 5)

Then powershell returns TRUE because the condition is satisfied. 5 is equal to 5. But if we add a NOT operator to this expression:

-not (5 -eq 5)

Then it turns the statement to its opposite and returns false. We can also write this statement as:

! (5 -eq 5)

Both gives us the same result FALSE because both are same -NOT operator.

Similarly if we write in powershell:

(5 -eq 6) -or (6 -eq 7)

Then powershell returns FALSE because none of the condition in the two expression is satisfied. But if we add a NOT operator to this expression:

!((5 -eq 6) -or (6 -eq 7))

Then it turns the result and returns a TRUE value.

Similarly if we write:

(5 -eq 5) -and (6 -eq 6)

Then powershell returns TRUE because both the conditions are satisfied. And if we add a NOT operator

!((5 -eq 6) -and (6 -eq 7))

Then powershell reverses the result and returns FALSE. Similarly if we write:

(5 -eq 5) -xor (6 -eq 7)

Then powershell returns TRUE because only one condition is satisfied. But if we add a NOT operator:

!((5 -eq 5) -xor (6 -eq 7))

Then powershell reverses the result and returns FALSE.

So I hope this would have made powershell logical operator concepts clear.

Logical operators help us to combine our statements and create complex expressions to test our results in a script or filter them according to our needs.

In next post we will learn about assignment operators.

If you like this post then do not forget to subscribe to our newsletter so that you can receive timely update whenever we publish any new post.

Originally published at https://toptechnotes.com on January 26, 2022.

--

--

Manish Chandra

I an an IT professional & owner of toptechnotes.com &have been working in IT industry for more than 15 years. Apart from work I love to read books.