1
00:00:01,040 --> 00:00:02,140
So let's begin.

2
00:00:02,140 --> 00:00:05,600
First up, let's take a look at comparison operator.

3
00:00:05,600 --> 00:00:10,970
With a comparison operator, we're going to compare two values.

4
00:00:10,970 --> 00:00:16,440
The operation will return a result of either True or False.

5
00:00:16,440 --> 00:00:18,070
Now when we're comparing things,

6
00:00:18,070 --> 00:00:20,490
if we're comparing numbers obviously that's pretty straightforward,

7
00:00:20,490 --> 00:00:25,730
if we're comparing strings, PowerShell by default is not case sensitive,

8
00:00:25,730 --> 00:00:27,970
although there are ways that you can make it case

9
00:00:27,970 --> 00:00:30,010
sensitive if you need to do that.

10
00:00:30,010 --> 00:00:32,200
And here's how these operators work.

11
00:00:32,200 --> 00:00:34,230
All of the comparison operators,

12
00:00:34,230 --> 00:00:36,740
and really all operators in general in PowerShell,

13
00:00:36,740 --> 00:00:41,400
always start with a dash and then whatever the operator name is.

14
00:00:41,400 --> 00:00:45,970
For example, in the comparison operator world there is greater than,

15
00:00:45,970 --> 00:00:50,720
and in PowerShell we use ‑gt. So is 5 ‑gt 2,

16
00:00:50,720 --> 00:00:53,830
and PowerShell will give you a result of True.

17
00:00:53,830 --> 00:00:59,380
Greater than or equal to, is 2 ‑ge 2? Well obviously it is,

18
00:00:59,380 --> 00:01:01,150
so the result is True.

19
00:01:01,150 --> 00:01:03,280
Less than, 5, is it ‑lt 2?

20
00:01:03,280 --> 00:01:06,860
And of course that's not the case, so we get False.

21
00:01:06,860 --> 00:01:08,550
Less than or equal to,

22
00:01:08,550 --> 00:01:12,640
so I hope you're beginning to see a pattern here on how these operators work.

23
00:01:12,640 --> 00:01:15,210
Nothing really cryptic or complicated.

24
00:01:15,210 --> 00:01:16,350
Let's move on.

25
00:01:16,350 --> 00:01:20,190
I'm going to set $i, remember that's a variable we looked at in the last module,

26
00:01:20,190 --> 00:01:22,330
and I'm going to give that a value of 7.

27
00:01:22,330 --> 00:01:25,840
The equal sign is the assignment operator.

28
00:01:25,840 --> 00:01:31,440
If you want to compare equality, that is the ‑eq operator.

29
00:01:31,440 --> 00:01:33,250
So is $i = 7?

30
00:01:33,250 --> 00:01:36,290
And yes, we get a result of True.

31
00:01:36,290 --> 00:01:40,620
If $i is not equal to 7, then we get a result of False.

32
00:01:40,620 --> 00:01:43,860
Now string comparisons, as I mentioned, are not case sensitive,

33
00:01:43,860 --> 00:01:48,920
so lowercase jeff is in fact equal to capital case JEFF,

34
00:01:48,920 --> 00:01:51,730
and PowerShell will give me a result of True.

35
00:01:51,730 --> 00:01:54,170
But if I want to make it case sensitive,

36
00:01:54,170 --> 00:01:56,840
there is a way with some of the equality operators to

37
00:01:56,840 --> 00:02:00,280
put a c in front of the operator, and that tells PowerShell,

38
00:02:00,280 --> 00:02:02,140
hey, make this case sensitive.

39
00:02:02,140 --> 00:02:09,070
So now lowercase jeff ‑ceq JEFF, that comparison is not valid,

40
00:02:09,070 --> 00:02:11,840
and so I get a result of False.

41
00:02:11,840 --> 00:02:17,720
The other way that we can do comparisons is with a wildcard operator like ‑like,

42
00:02:17,720 --> 00:02:20,480
and here we use the wildcard character, the asterisk.

43
00:02:20,480 --> 00:02:27,580
So is the string foo ‑like f, and then whatever follows after the letter f,

44
00:02:27,580 --> 00:02:29,570
and of course that is True.

45
00:02:29,570 --> 00:02:33,500
To do the opposite comparison, we can use ‑notlike.

46
00:02:33,500 --> 00:02:37,800
So here we have an example where bar is ‑notlike B*,

47
00:02:37,800 --> 00:02:40,460
and even though I'm using a capital letter,

48
00:02:40,460 --> 00:02:42,710
again, comparisons are not case sensitive,

49
00:02:42,710 --> 00:02:45,070
and so I get a result of False.

50
00:02:45,070 --> 00:02:46,900
Like I showed you in the last slide,

51
00:02:46,900 --> 00:02:51,010
we can make it case sensitive by putting that c in front of it,

52
00:02:51,010 --> 00:02:59,940
so now I do bar ‑clike B*, and again that's going to be False.

53
00:02:59,940 --> 00:03:03,680
There is also a way to match regular expressions in

54
00:03:03,680 --> 00:03:06,740
PowerShell with the ‑match operator.

55
00:03:06,740 --> 00:03:09,030
So here I can take a string on the left side,

56
00:03:09,030 --> 00:03:15,430
abc‑1234, and say does it match a regular expression operator of \d+.

57
00:03:15,430 --> 00:03:19,910
So I need one or more digit characters, is it in that string,

58
00:03:19,910 --> 00:03:23,620
and because it is, I get a result of True.

59
00:03:23,620 --> 00:03:26,930
Now, I'll admit this is an advanced topic,

60
00:03:26,930 --> 00:03:29,050
we will not be getting into regular expressions,

61
00:03:29,050 --> 00:03:32,260
but if you already understand how regular expressions

62
00:03:32,260 --> 00:03:36,510
work and how to build a pattern, you can use the ‑match operator.

63
00:03:36,510 --> 00:03:40,250
There is a help topic you can look at if you want a refresher to get started,

64
00:03:40,250 --> 00:03:42,370
Help About_Regular_Expressions,

65
00:03:42,370 --> 00:03:46,100
and there are some other courses in the Pluralsight

66
00:03:46,100 --> 00:03:49,830
library on PowerShell and regular expressions if you want

67
00:03:49,830 --> 00:03:52,390
to dive into this topic further.

68
00:03:52,390 --> 00:03:57,130
You also have a way to test a non‑match with the ‑notmatch operator,

69
00:03:57,130 --> 00:03:59,910
so 1234 does it ‑notmatch.

70
00:03:59,910 --> 00:04:03,140
Again, on the right side is the regular expression pattern,

71
00:04:03,140 --> 00:04:04,430
one or more digits,

72
00:04:04,430 --> 00:04:09,850
and that gives the result of False because there are digits in that string.

73
00:04:09,850 --> 00:04:13,710
Here's an example of how you might use a comparison operator in PowerShell.

74
00:04:13,710 --> 00:04:15,360
So I'm going to do Get‑Process,

75
00:04:15,360 --> 00:04:20,530
and then get all the processes where the company name matches Logitech,

76
00:04:20,530 --> 00:04:21,610
and then I'm selecting some properties.

77
00:04:21,610 --> 00:04:23,910
So you can see there are a couple of processes,

78
00:04:23,910 --> 00:04:27,630
and in some processes it's using Logitech,

79
00:04:27,630 --> 00:04:29,980
and sometimes it's using Logitech, Inc.

80
00:04:29,980 --> 00:04:34,110
So the regular expression operator ‑match allows me to get

81
00:04:34,110 --> 00:04:36,740
all the processes from a related company.

82
00:04:36,740 --> 00:04:40,140
And I'm kind of doing the same thing with the second example,

83
00:04:40,140 --> 00:04:44,300
where I'm getting all the processes where the name of the company

84
00:04:44,300 --> 00:04:47,150
does not match something that starts with micro,

85
00:04:47,150 --> 00:04:49,900
like Microsoft, and then grouping them on the company,

86
00:04:49,900 --> 00:04:53,360
so you can see all of the other company names associated with

87
00:04:53,360 --> 00:04:55,730
the processes running on this computer.

88
00:04:55,730 --> 00:04:56,270
Again,

89
00:04:56,270 --> 00:05:03,000
this is a nice way of showing you why and how you might want to use these comparison operators.

