1
00:00:02,340 --> 00:00:05,280
So let's hop back into PowerShell, go to a prompt,

2
00:00:05,280 --> 00:00:09,840
and let me demonstrate these filtering concepts and techniques.

3
00:00:09,840 --> 00:00:13,260
Alright, once again, let's turn to Get‑Vegetable,

4
00:00:13,260 --> 00:00:18,420
my command that we can use to demonstrate and illustrate these filtering ideas.

5
00:00:18,420 --> 00:00:21,620
If you've been jumping around and haven't seen this command before,

6
00:00:21,620 --> 00:00:24,240
it's part of a module called PS Teaching Tools,

7
00:00:24,240 --> 00:00:27,840
and I have a note in the download file that will explain how you can

8
00:00:27,840 --> 00:00:31,140
install the module so you can get this command.

9
00:00:31,140 --> 00:00:32,640
I'll go ahead and run it.

10
00:00:32,640 --> 00:00:35,430
And as we have seen before in the in this course,

11
00:00:35,430 --> 00:00:39,640
this gives me then a collection of vegetable objects.

12
00:00:39,640 --> 00:00:42,640
If I look at help for Get‑Vegetable,

13
00:00:42,640 --> 00:00:46,440
I can see that there are a few parameters that I can use to limit,

14
00:00:46,440 --> 00:00:49,630
or restrict, or filter, if you will,

15
00:00:49,630 --> 00:00:53,340
the information that that command is going to generate.

16
00:00:53,340 --> 00:00:58,680
So let's try this with the name parameter and just get,

17
00:00:58,680 --> 00:01:00,970
say, just radishes, and sure enough,

18
00:01:00,970 --> 00:01:03,640
that works just the way that I would expect it to.

19
00:01:03,640 --> 00:01:05,780
I can even refine this and say, you know what,

20
00:01:05,780 --> 00:01:10,240
show me all the vegetables where the name starts with r,

21
00:01:10,240 --> 00:01:13,440
and sure enough, it does.

22
00:01:13,440 --> 00:01:15,600
Now let's look at PowerShell commands.

23
00:01:15,600 --> 00:01:19,040
Let's look at Get‑Service.

24
00:01:19,040 --> 00:01:19,980
And as we can see,

25
00:01:19,980 --> 00:01:22,930
there are a number of parameters that looks like they'll help us

26
00:01:22,930 --> 00:01:27,730
limit or filter the services from the very beginning.

27
00:01:27,730 --> 00:01:31,250
So let's do Get‑Service, and let's do m*,

28
00:01:31,250 --> 00:01:34,360
find all the services that start with m.

29
00:01:34,360 --> 00:01:35,770
That's pretty good, pretty easy.

30
00:01:35,770 --> 00:01:38,110
There's another parameter just to kind of

31
00:01:38,110 --> 00:01:41,440
demonstrate how these things might work.

32
00:01:41,440 --> 00:01:43,180
There is an exclude parameter.

33
00:01:43,180 --> 00:01:47,350
So let's say, you know what, I want to get all the services that start with m,

34
00:01:47,350 --> 00:01:50,640
but I don't want the one to start with ms.

35
00:01:50,640 --> 00:01:56,050
So let's do get‑service m*, now, the name parameter is positional,

36
00:01:56,050 --> 00:01:58,690
that's why I didn't type it in, Exclude,

37
00:01:58,690 --> 00:01:59,180
though,

38
00:01:59,180 --> 00:02:04,090
I have to type the name and tell it to exclude any service that starts with ms,

39
00:02:04,090 --> 00:02:06,660
and now I get a simpler list.

40
00:02:06,660 --> 00:02:09,190
So that all works the way that I expect it to.

41
00:02:09,190 --> 00:02:12,940
Good examples of early filtering.

42
00:02:12,940 --> 00:02:16,430
I want to show you some early filtering with the file system and

43
00:02:16,430 --> 00:02:19,780
with Get‑ChildItem, or the dir alias. Because this is the stuff

44
00:02:19,780 --> 00:02:21,930
that you're going to see a lot, and it will confuse you,

45
00:02:21,930 --> 00:02:25,340
so let me take a few minutes to go through this.

46
00:02:25,340 --> 00:02:28,010
If I do directory of my scripts folder, say,

47
00:02:28,010 --> 00:02:30,770
show me all the ps1 files in scripts.

48
00:02:30,770 --> 00:02:36,640
So there we go, so that's all the files in the root of my scripts folder.

49
00:02:36,640 --> 00:02:40,490
When you look at help, you'll also see, oh, there's a filter parameter.

50
00:02:40,490 --> 00:02:41,310
That's kind of cool.

51
00:02:41,310 --> 00:02:43,100
Maybe that will be very helpful.

52
00:02:43,100 --> 00:02:44,920
Jeff was talking about filtering early.

53
00:02:44,920 --> 00:02:46,800
There's an option to filter.

54
00:02:46,800 --> 00:02:49,640
Cool, that seems to work as well.

55
00:02:49,640 --> 00:02:52,070
There's also, if you look at help,

56
00:02:52,070 --> 00:02:55,970
you'll see that Get‑ChildItem has an include parameter.

57
00:02:55,970 --> 00:02:58,520
So you might think, oh, I'll just try this.

58
00:02:58,520 --> 00:03:02,140
I'll do include *.ps1.

59
00:03:02,140 --> 00:03:03,740
I don't see anything.

60
00:03:03,740 --> 00:03:07,610
Well, that's because there's actually a little trick here with this,

61
00:03:07,610 --> 00:03:10,240
and that's why you have to read the help.

62
00:03:10,240 --> 00:03:12,440
Let me rerun the command.

63
00:03:12,440 --> 00:03:14,570
So we're going to include .ps1, and you know what,

64
00:03:14,570 --> 00:03:18,050
I'm going to also exclude that trial.ps1 that you see there.

65
00:03:18,050 --> 00:03:23,060
For the include and exclude, you actually also have to Recurse,

66
00:03:23,060 --> 00:03:25,640
and now that works.

67
00:03:25,640 --> 00:03:28,780
So whether you use the filter, the path,

68
00:03:28,780 --> 00:03:32,140
the name, the include, or exclude really all depends upon,

69
00:03:32,140 --> 00:03:36,840
not only what you're trying to do, but maybe where you're trying to do it.

70
00:03:36,840 --> 00:03:38,480
Let me give you another example here.

71
00:03:38,480 --> 00:03:42,440
PowerShell has other drives other than the file system, you know,

72
00:03:42,440 --> 00:03:44,230
things like the registry, and the functions,

73
00:03:44,230 --> 00:03:45,840
and aliases.

74
00:03:45,840 --> 00:03:49,340
So I'm going to just use the alias PSDrive.

75
00:03:49,340 --> 00:03:52,590
It's the same command, Get‑ChildItem, and you can use the dir alias,

76
00:03:52,590 --> 00:03:53,400
and I'm saying,

77
00:03:53,400 --> 00:03:59,700
look at the alias PSDrive and find me all the aliases that start with c,

78
00:03:59,700 --> 00:04:02,920
and I get an error message.

79
00:04:02,920 --> 00:04:07,120
And in fact, it makes it very clear, Get‑ChildItem cannot call that method.

80
00:04:07,120 --> 00:04:09,760
The provider does not support the use of filters.

81
00:04:09,760 --> 00:04:15,640
What that means is that the alias provider doesn't support filtering.

82
00:04:15,640 --> 00:04:18,630
Because filtering, there's a certain technical,

83
00:04:18,630 --> 00:04:21,580
programmatic way that filtering is described,

84
00:04:21,580 --> 00:04:25,340
and the filter parameter must use that,

85
00:04:25,340 --> 00:04:28,360
and Get‑ChildItem can't work in the alias with that filter.

86
00:04:28,360 --> 00:04:31,940
But that's fine because we have alternatives, right?

87
00:04:31,940 --> 00:04:34,440
We could do ‑name.

88
00:04:34,440 --> 00:04:36,340
That doesn't seem to work either.

89
00:04:36,340 --> 00:04:39,140
But see, you've got to try these things.

90
00:04:39,140 --> 00:04:41,740
Let's try the include trick.

91
00:04:41,740 --> 00:04:43,820
I still have to use Recurse though.

92
00:04:43,820 --> 00:04:52,240
Now that works, although something like this using the path might be even easier.

93
00:04:52,240 --> 00:04:57,210
So there are a number of ways that you can filter using different parameters,

94
00:04:57,210 --> 00:05:01,740
but you may have to experiment to find the right combination of

95
00:05:01,740 --> 00:05:03,800
parameters to get you the results you want,

96
00:05:03,800 --> 00:05:06,440
maybe with the least amount of work.

97
00:05:06,440 --> 00:05:11,540
Now let me give you another example of where reading the help is very important.

98
00:05:11,540 --> 00:05:14,980
So I'm going to run get‑ciminstance and get all of the

99
00:05:14,980 --> 00:05:19,740
Win32_SystemDriver instances on my computer.

100
00:05:19,740 --> 00:05:24,120
Alright, so I get a lot of good information, but it was more though than I want.

101
00:05:24,120 --> 00:05:24,500
Really,

102
00:05:24,500 --> 00:05:28,060
all I care about is I just want to see the ones where they're

103
00:05:28,060 --> 00:05:33,040
running and the servicetype is the file system driver.

104
00:05:33,040 --> 00:05:36,230
So get‑ciminstance has a filter parameter,

105
00:05:36,230 --> 00:05:40,040
which is basically like a where clause in the SQL query.

106
00:05:40,040 --> 00:05:44,540
Type this out here.

107
00:05:44,540 --> 00:05:44,910
Alright,

108
00:05:44,910 --> 00:05:47,410
So what I'm doing with the command here is

109
00:05:47,410 --> 00:05:52,640
get‑ciminstances from that Win32_SystemDriver class,

110
00:05:52,640 --> 00:05:54,010
and then the filter is basically,

111
00:05:54,010 --> 00:05:59,960
get me everything where the state property is equal to running and the

112
00:05:59,960 --> 00:06:04,960
servicetype is equal to file system driver. Query that I'm doing here,

113
00:06:04,960 --> 00:06:12,700
this is a WQL query, a WMI type query, this uses the traditional

114
00:06:12,700 --> 00:06:15,940
operators. Notice I'm using the equal sign.

115
00:06:15,940 --> 00:06:18,120
I'm not using a PowerShell operator,

116
00:06:18,120 --> 00:06:24,840
so the filter is generating a query that will limit kind of like a SQL

117
00:06:24,840 --> 00:06:29,140
query and return back just the information that I want.

118
00:06:29,140 --> 00:06:32,420
This becomes super important when you start scripting at scale.

119
00:06:32,420 --> 00:06:37,440
If I were to run this command against 500 servers and bring the results back,

120
00:06:37,440 --> 00:06:42,390
I want to have those 500 servers limit at the source and not

121
00:06:42,390 --> 00:06:50,000
bring everything back to me, and then I filter. That's why early filtering is so important.

