1
00:00:01,140 --> 00:00:05,320
Logical operators come into play when you have more complex expressions.

2
00:00:05,320 --> 00:00:07,100
So let me walk you through this.

3
00:00:07,100 --> 00:00:12,640
I'm going to start by assigning $i with a value of 4.

4
00:00:12,640 --> 00:00:14,800
And now I want to have an expression.

5
00:00:14,800 --> 00:00:16,500
I'll admit this is a little artificial,

6
00:00:16,500 --> 00:00:19,640
but I want to demonstrate the logical operator.

7
00:00:19,640 --> 00:00:22,120
The logical operator is ‑AND.

8
00:00:22,120 --> 00:00:25,230
There's no case sensitivity here with the operator.

9
00:00:25,230 --> 00:00:28,320
I just happened to put it in all uppercase so it stands

10
00:00:28,320 --> 00:00:31,100
out so I can see that it is an operator.

11
00:00:31,100 --> 00:00:33,080
So here's how the operator works.

12
00:00:33,080 --> 00:00:38,340
All of the expressions are evaluated, and then the ‑AND operator says,

13
00:00:38,340 --> 00:00:41,740
hey, if all of the expressions are true,

14
00:00:41,740 --> 00:00:45,500
then the whole expression has a result of true.

15
00:00:45,500 --> 00:00:47,640
Otherwise, the whole expression is false.

16
00:00:47,640 --> 00:00:49,820
So I've got two expressions here.

17
00:00:49,820 --> 00:00:53,230
I have a $i is less than or equal to 10.

18
00:00:53,230 --> 00:00:56,270
And we know that that is true because 4 is less than or equal to 10.

19
00:00:56,270 --> 00:01:00,810
And I want to know is the PSVersionTable variable,

20
00:01:00,810 --> 00:01:04,300
and then the PSVersion property, which is an object,

21
00:01:04,300 --> 00:01:07,340
and that object has another property called Major,

22
00:01:07,340 --> 00:01:11,650
which will be a number, is that number greater or equal to 7?

23
00:01:11,650 --> 00:01:17,040
Assuming I'm then running this on PowerShell 7, that will also be true.

24
00:01:17,040 --> 00:01:24,040
So I've got true and true, which means the entire expression will be true.

25
00:01:24,040 --> 00:01:26,660
And, of course, if one of the comparisons is false,

26
00:01:26,660 --> 00:01:28,840
then the entire expression is false.

27
00:01:28,840 --> 00:01:32,730
So now I have $i = 20, $i less than equal to 10.

28
00:01:32,730 --> 00:01:33,870
That is false.

29
00:01:33,870 --> 00:01:39,860
So even though the PSVersionTable.Major value is greater than or equal to 7,

30
00:01:39,860 --> 00:01:44,940
I have false and true, which means the entire expression is false.

31
00:01:44,940 --> 00:01:47,360
Related to this is the ‑OR operator.

32
00:01:47,360 --> 00:01:53,520
Same idea, I'm going to compare or evaluate all of these individual expressions.

33
00:01:53,520 --> 00:01:59,140
As long as one of them is true, then the entire expression is true.

34
00:01:59,140 --> 00:02:02,460
So now, even though $i less than or equal to 10,

35
00:02:02,460 --> 00:02:08,060
even though that part is false, because the PSVersionTable expression is true,

36
00:02:08,060 --> 00:02:10,310
the entire expression is true.

37
00:02:10,310 --> 00:02:11,300
And in PowerShell,

38
00:02:11,300 --> 00:02:14,660
you can also combine these logical operators if you have a really

39
00:02:14,660 --> 00:02:18,140
complex expression that you're trying to evaluate.

40
00:02:18,140 --> 00:02:21,540
So here I have a variable $1 = 20.

41
00:02:21,540 --> 00:02:23,960
I'm setting $name = "jeff".

42
00:02:23,960 --> 00:02:29,220
And now I have a complex expression, is $i greater than or equal to 20,

43
00:02:29,220 --> 00:02:33,660
and now I have a second part of that complex expression,

44
00:02:33,660 --> 00:02:38,990
is $name = "Jeff", or am I running a Linux machine?

45
00:02:38,990 --> 00:02:43,500
$IsLinux is a new PowerShell variable that will be true or false if I'm running

46
00:02:43,500 --> 00:02:46,540
on a Linux box that will have a value of true; otherwise,

47
00:02:46,540 --> 00:02:49,140
it will be false.

48
00:02:49,140 --> 00:02:53,600
If both sides of the ‑AND operation are true,

49
00:02:53,600 --> 00:02:58,190
then I have a complete result of true and PowerShell gives that to me.

50
00:02:58,190 --> 00:03:02,430
This is clearly a situation where parentheses really come in handy.

51
00:03:02,430 --> 00:03:05,000
Now they're not technically required,

52
00:03:05,000 --> 00:03:08,520
but they will make it easier for you to understand what PowerShell is doing,

53
00:03:08,520 --> 00:03:11,850
and you're less likely to run into an error or not get a

54
00:03:11,850 --> 00:03:15,240
result that is appropriate or correct.

55
00:03:15,240 --> 00:03:18,430
So what I'm doing here to reinforce the idea,

56
00:03:18,430 --> 00:03:23,070
so I've got the ‑AND operation has two things going on.

57
00:03:23,070 --> 00:03:25,140
I have $i greater than or equal to 20,

58
00:03:25,140 --> 00:03:27,890
and you can see the parentheses there I've colorized,

59
00:03:27,890 --> 00:03:32,700
‑AND, and then the set of outer parentheses controls the next expression,

60
00:03:32,700 --> 00:03:35,260
which is a nested ‑OR comparison.

61
00:03:35,260 --> 00:03:39,710
So I already know the left side of the ‑AND operation is true

62
00:03:39,710 --> 00:03:42,100
because 20 is greater than or equal to 20.

63
00:03:42,100 --> 00:03:45,540
Now on the right side in the ‑OR operation,

64
00:03:45,540 --> 00:03:53,250
I'm comparing is $name = "jeff", and we know that that is true,

65
00:03:53,250 --> 00:03:55,570
or am I running Linux?

66
00:03:55,570 --> 00:03:58,860
Now if I'm running this on a Linux box, that's going to be true,

67
00:03:58,860 --> 00:04:01,180
or if I'm running this on Windows, that's going to be false.

68
00:04:01,180 --> 00:04:04,830
But it really doesn't matter because as long as one of the expressions in

69
00:04:04,830 --> 00:04:08,870
the ‑OR operation that's inside the yellow parentheses,

70
00:04:08,870 --> 00:04:13,510
as long as one of those is true, then the whole operation is true.

71
00:04:13,510 --> 00:04:16,620
So I've got true on the left, true on the right,

72
00:04:16,620 --> 00:04:20,140
and so the whole operation then becomes true.

73
00:04:20,140 --> 00:04:22,940
Hope you kind of followed all of that.

74
00:04:22,940 --> 00:04:25,590
Another logical operator is ‑NOT.

75
00:04:25,590 --> 00:04:28,660
This can be a little confusing until you kind of

76
00:04:28,660 --> 00:04:30,340
get your head around the concept.

77
00:04:30,340 --> 00:04:32,480
So here's an expression.

78
00:04:32,480 --> 00:04:36,820
I want to use Test‑Path and verify that Windows Notepad actually exists,

79
00:04:36,820 --> 00:04:37,780
and it does.

80
00:04:37,780 --> 00:04:39,450
And so I get a value of true.

81
00:04:39,450 --> 00:04:42,140
That is the normal expected result.

82
00:04:42,140 --> 00:04:45,550
However, I can use the ‑NOT operator,

83
00:04:45,550 --> 00:04:51,940
and that will give me a result that reverses whatever the Boolean value is.

84
00:04:51,940 --> 00:04:57,350
So my expression becomes ‑NOT, and I find this easier to put in parentheses.

85
00:04:57,350 --> 00:05:00,260
If this whole expression or this Test‑Path,

86
00:05:00,260 --> 00:05:04,050
whatever that value is, the ‑NOT is going to reverse it.

87
00:05:04,050 --> 00:05:07,930
So because I already know that Test‑Path is going to return true,

88
00:05:07,930 --> 00:05:09,240
‑NOT reverses that.

89
00:05:09,240 --> 00:05:13,540
And so that gives me false as the result.

90
00:05:13,540 --> 00:05:18,300
Sometimes this becomes a handy little tool if you want to test

91
00:05:18,300 --> 00:05:20,640
for something and then reverse the result.

92
00:05:20,640 --> 00:05:23,140
That's what ‑NOT will do for you.

93
00:05:23,140 --> 00:05:28,870
The ‑NOT operator also has an alias of the exclamation point or bang

94
00:05:28,870 --> 00:05:32,390
symbol so that you can use that instead of ‑NOT.

95
00:05:32,390 --> 00:05:35,570
I tend to like using ‑NOT because that's clear.

96
00:05:35,570 --> 00:05:38,740
The exclamation point is a little more cryptic.

97
00:05:38,740 --> 00:05:41,220
You have to kind of know in advance what that means.

98
00:05:41,220 --> 00:05:46,010
‑NOT absolutely, hopefully, makes a little more sense to you.

99
00:05:46,010 --> 00:05:53,000
The ‑NOT operator is something that you probably will use a lot more when you get into PowerShell scripting.

