Logical operators come into play when you have more complex expressions.
So let me walk you through this.
I'm going to start by assigning $i with a value of 4.
And now I want to have an expression.
I'll admit this is a little artificial,
but I want to demonstrate the logical operator.
The logical operator is ‑AND.
There's no case sensitivity here with the operator.
I just happened to put it in all uppercase so it stands
out so I can see that it is an operator.
So here's how the operator works.
All of the expressions are evaluated, and then the ‑AND operator says,
hey, if all of the expressions are true,
then the whole expression has a result of true.
Otherwise, the whole expression is false.
So I've got two expressions here.
I have a $i is less than or equal to 10.
And we know that that is true because 4 is less than or equal to 10.
And I want to know is the PSVersionTable variable,
and then the PSVersion property, which is an object,
and that object has another property called Major,
which will be a number, is that number greater or equal to 7?
Assuming I'm then running this on PowerShell 7, that will also be true.
So I've got true and true, which means the entire expression will be true.
And, of course, if one of the comparisons is false,
then the entire expression is false.
So now I have $i = 20, $i less than equal to 10.
That is false.
So even though the PSVersionTable.Major value is greater than or equal to 7,
I have false and true, which means the entire expression is false.
Related to this is the ‑OR operator.
Same idea, I'm going to compare or evaluate all of these individual expressions.
As long as one of them is true, then the entire expression is true.
So now, even though $i less than or equal to 10,
even though that part is false, because the PSVersionTable expression is true,
the entire expression is true.
And in PowerShell,
you can also combine these logical operators if you have a really
complex expression that you're trying to evaluate.
So here I have a variable $1 = 20.
I'm setting $name = "jeff".
And now I have a complex expression, is $i greater than or equal to 20,
and now I have a second part of that complex expression,
is $name = "Jeff", or am I running a Linux machine?
$IsLinux is a new PowerShell variable that will be true or false if I'm running
on a Linux box that will have a value of true; otherwise,
it will be false.
If both sides of the ‑AND operation are true,
then I have a complete result of true and PowerShell gives that to me.
This is clearly a situation where parentheses really come in handy.
Now they're not technically required,
but they will make it easier for you to understand what PowerShell is doing,
and you're less likely to run into an error or not get a
result that is appropriate or correct.
So what I'm doing here to reinforce the idea,
so I've got the ‑AND operation has two things going on.
I have $i greater than or equal to 20,
and you can see the parentheses there I've colorized,
‑AND, and then the set of outer parentheses controls the next expression,
which is a nested ‑OR comparison.
So I already know the left side of the ‑AND operation is true
because 20 is greater than or equal to 20.
Now on the right side in the ‑OR operation,
I'm comparing is $name = "jeff", and we know that that is true,
or am I running Linux?
Now if I'm running this on a Linux box, that's going to be true,
or if I'm running this on Windows, that's going to be false.
But it really doesn't matter because as long as one of the expressions in
the ‑OR operation that's inside the yellow parentheses,
as long as one of those is true, then the whole operation is true.
So I've got true on the left, true on the right,
and so the whole operation then becomes true.
Hope you kind of followed all of that.
Another logical operator is ‑NOT.
This can be a little confusing until you kind of
get your head around the concept.
So here's an expression.
I want to use Test‑Path and verify that Windows Notepad actually exists,
and it does.
And so I get a value of true.
That is the normal expected result.
However, I can use the ‑NOT operator,
and that will give me a result that reverses whatever the Boolean value is.
So my expression becomes ‑NOT, and I find this easier to put in parentheses.
If this whole expression or this Test‑Path,
whatever that value is, the ‑NOT is going to reverse it.
So because I already know that Test‑Path is going to return true,
‑NOT reverses that.
And so that gives me false as the result.
Sometimes this becomes a handy little tool if you want to test
for something and then reverse the result.
That's what ‑NOT will do for you.
The ‑NOT operator also has an alias of the exclamation point or bang
symbol so that you can use that instead of ‑NOT.
I tend to like using ‑NOT because that's clear.
The exclamation point is a little more cryptic.
You have to kind of know in advance what that means.
‑NOT absolutely, hopefully, makes a little more sense to you.
The ‑NOT operator is something that you probably will use a lot more when you get into PowerShell scripting.