I don’t know what category assign to this article. In my opinion, the knowledge I wrote about is something between basics and medium level. I’m sure that sometimes you need to compare more than two variables. Powershell as any programming language will help you with this. It’s time to explain Powershell logical operators.
Before you start reading more, please read my article about comparison operators. It’s required to know comparison operators before you will begin learning about Powershell Logical Operators.
Logical operators
First of all, you have to learn and understand logical expressions. This is required to write advanced Powershell code. A long time I was thinking of how to explain it to you. I decided to try to explain it with real-life examples. I think it will be more transparent.
Logical expressions help to check in one condition more than one comparison.
To use logical expression, you need operators. I will explain the basics operators in examples.
Operator AND
Please imagine your shopping. When you find a product what you want to buy. You can check if the price is $1 and check if you have enough money. If all is true, then you can buy it. In the other situation you can’t buy it.
The price is $1 | YES (true) | YES (true) | NO, the price is $2 (false) | NO, the price is $2 (false) |
You have only $1 | YES (true) | NO (false) | Yes (true) | NO (false) |
RESULT | You can buy (true) | You can’t buy (false) | You can’t buy (false) | You can’t buy (false) |
$TheProductPrice1 = 1
$MyMoney = 1
$TheProductPrice1 -eq 1 -and $MyMoney -eq 1
$TheProductPrice1 = 2
$MyMoney = 1
$TheProductPrice1 -eq 1 -and $MyMoney -eq 1
$TheProductPrice1 = 1
$MyMoney = 0
$TheProductPrice1 -eq 1 -and $MyMoney -eq 1
$TheProductPrice1 = 2
$MyMoney = 0
$TheProductPrice1 -eq 1 -and $MyMoney -eq 1

Operator OR
You can use this operator when you want to check if one of the conditions is true. Let’s show this in the new but similar example. You have $1, and you want to buy one of two products with it. As a result, you need to check which one you can buy.
The first product price is $1 | YES (true) | YES (true) | NO (false) | NO (false) |
The second product price is $1 | YES (true) | NO (false) | Yes (true) | NO (false) |
RESULT | You can buy one of them (true) | You can buy one of them (true) | You can buy one of them (true) | You can’t buy it (false) |
$TheProductPrice1 = 1
$TheProductPrice2 = 1
$TheProductPrice1 -eq 1 -or $TheProductPrice2 -eq 1
$TheProductPrice1 = 2
$TheProductPrice2 = 1
$TheProductPrice1 -eq 1 -or $TheProductPrice2 -eq 1
$TheProductPrice1 = 1
$TheProductPrice2 = 0
$TheProductPrice1 -eq 1 -or $TheProductPrice2 -eq 1
$TheProductPrice1 = 2
$TheProductPrice2 = 0
$TheProductPrice1 -eq 1 -or $TheProductPrice2 -eq 1

Operator XOR
A long time I was searching for the best example to explain this operator. The XOR operator is something like logical exclusive OR so I will show you the same table as above, but a little modified. If every single product price is $1, you don’t want to buy any of them. The result will be true if only one of the products price is $1 and the second is not.
The first product price is $1 | YES (true) | YES (true) | NO (false) | NO (false) |
The second product price is $1 | YES (true) | NO (false) | Yes (true) | NO (false) |
RESULT | You don’t want to buy (false) | You can buy one of them (true) | You can buy one of them (true) | You can’t buy it (false) |
$TheProductPrice1 = 1
$TheProductPrice2 = 1
$TheProductPrice1 -eq 1 -xor $TheProductPrice2 -eq 1
$TheProductPrice1 = 2
$TheProductPrice2 = 1
$TheProductPrice1 -eq 1 -xor $TheProductPrice2 -eq 1
$TheProductPrice1 = 1
$TheProductPrice2 = 0
$TheProductPrice1 -eq 1 -xor $TheProductPrice2 -eq 1
$TheProductPrice1 = 2
$TheProductPrice2 = 0
$TheProductPrice1 -eq 1 -xor $TheProductPrice2 -eq 1

Operator NOT (!)
It’s the last one which I would like to demonstrate to you. This operator negates the result of the comparison. Let’s see this in the example below.
The first product price is $1 | True | False |
RESULT | False | True |
$TheProductPrice1 = 1
-not ($TheProductPrice1 -eq 1)
!($TheProductPrice1 -eq 1)

Why you should learn Logical Operators?
All programming languages let us compare more than two pair of value. As a result, If you want to write advanced logical Powershell scripts, you have to know this knowledge. You can use this in the conditions to use one IF statements than more. I wrote about Powershell conditions in the post How breakfast can help you to understand Powershell conditions.
Have you any questions? Please leave a comment below.