Hi, Damian Garbus here and this is the next post about the basics of PowerShell. It’s time to write about PowerShell Logical Operators. It’s an important topic when you start using PowerShell conditions. If you check out the Powershell basics for everyone, you should also know comparison operators.
What are PowerShell Logical Operators?
When you compare two values using comparison operators, PowerShell will give you an answer: true or false. The result is nothing else than boolean an object type which I shortly described in the post about PowerShell variable types. It becomes more difficult when you want to compare more than two values. Then you can use PowerShell Logical Operators.
Real-life example
Let’s see where you can use PowerShell Logical Operators in real life situations. My first thought was online shopping. Let’s do this 🙂
So, let’s imagine that you want to buy a new book about PowerShell (why not 🙂 ) Let’s use PowerShell to compare three book prices.
First Book – $30
Second Book – $50
Third Book – $70
Without using logical operators first, you have to check if ‘’First Book’’ is cheaper than the ‘’Second Book’’. If it’s true, you have to check if ’’First Book’’ is cheaper than the ‘’Third Book’’.
$FirstBookPrice = 30
$SecondBookPrice = 50
$ThirdBookPrice = 60
$FirstBookPrice -lt $SecondBookPrice
$FirstBookPrice -lt $ThirdBookPrice

You can use PowerShell Logical Operator “-and” to check the same values in one condition. In the result Powershell return you true for above two conditions.
$FirstBookPrice -lt $SecondBookPrice -and $FirstBookPrice -lt $ThirdBookPrice

What’s next?
Let’s see all of PowerShell Logical Operators and which of the data returns. It’s important to understand combinations of true/false.
-and – All comparisons have to be true then PowerShell returns true
– true and true returns true
– true and false returns false
– false and false returns false
-or – One of the comparisons have to be true then PowerShell returns true
– true and true returns true
– true and false returns true
-false and false returns false
-xor – Gives true only in the situation when two value not match and one of them is true.
– true and true returns false
– true and false returns true
– false and false returns true
-not – false returns
! – the same as -not
Why is it good to know PowerShell Logical Operators?
One of the advantages is that your code will be more readable. Please look at the next example with book prices.
How to find the cheapest book without using logical operators?
param (
[int]$FirstBookPrice,
[int]$SecondBookPrice,
[int]$ThirdBookPrice
)
if ($FirstBookPrice -lt $SecondBookPrice) {
if ($FirstBookPrice -lt $ThirdBookPrice) {
Write-Host "First book is cheapest"
}
}
elseif ($SecondBookPrice -lt $ThirdBookPrice) {
if ($SecondBookPrice -lt $FirstBookPrice) {
Write-Host "Second book is cheapest"
}
}
else {
Write-Host "Third book is cheapest"
}

Now, please look at the code when you use logical operators. It gives you the same result.
param (
[int]$FirstBookPrice,
[int]$SecondBookPrice,
[int]$ThirdBookPrice
)
if ($FirstBookPrice -lt $SecondBookPrice -and $FirstBookPrice -lt $ThirdBookPrice) {
Write-Host "First book is cheapest"
}
elseif ($SecondBookPrice -lt $ThirdBookPrice -and $SecondBookPrice -lt $FirstBookPrice) {
Write-Host "Second book is cheapest"
}
else {
Write-Host "Third book is cheapest"
}

Conclusion
PowerShell Logical Operators is one of the most difficult topic to understand for PowerShell beginners. I hope that my explanation is understandable. Please leave your comment and let me know which aspect of PowerShell is the hardest to understand for you.