So let's hop back into PowerShell, go to a prompt,
and let me demonstrate these filtering concepts and techniques.
Alright, once again, let's turn to Get‑Vegetable,
my command that we can use to demonstrate and illustrate these filtering ideas.
If you've been jumping around and haven't seen this command before,
it's part of a module called PS Teaching Tools,
and I have a note in the download file that will explain how you can
install the module so you can get this command.
I'll go ahead and run it.
And as we have seen before in the in this course,
this gives me then a collection of vegetable objects.
If I look at help for Get‑Vegetable,
I can see that there are a few parameters that I can use to limit,
or restrict, or filter, if you will,
the information that that command is going to generate.
So let's try this with the name parameter and just get,
say, just radishes, and sure enough,
that works just the way that I would expect it to.
I can even refine this and say, you know what,
show me all the vegetables where the name starts with r,
and sure enough, it does.
Now let's look at PowerShell commands.
Let's look at Get‑Service.
And as we can see,
there are a number of parameters that looks like they'll help us
limit or filter the services from the very beginning.
So let's do Get‑Service, and let's do m*,
find all the services that start with m.
That's pretty good, pretty easy.
There's another parameter just to kind of
demonstrate how these things might work.
There is an exclude parameter.
So let's say, you know what, I want to get all the services that start with m,
but I don't want the one to start with ms.
So let's do get‑service m*, now, the name parameter is positional,
that's why I didn't type it in, Exclude,
though,
I have to type the name and tell it to exclude any service that starts with ms,
and now I get a simpler list.
So that all works the way that I expect it to.
Good examples of early filtering.
I want to show you some early filtering with the file system and
with Get‑ChildItem, or the dir alias. Because this is the stuff
that you're going to see a lot, and it will confuse you,
so let me take a few minutes to go through this.
If I do directory of my scripts folder, say,
show me all the ps1 files in scripts.
So there we go, so that's all the files in the root of my scripts folder.
When you look at help, you'll also see, oh, there's a filter parameter.
That's kind of cool.
Maybe that will be very helpful.
Jeff was talking about filtering early.
There's an option to filter.
Cool, that seems to work as well.
There's also, if you look at help,
you'll see that Get‑ChildItem has an include parameter.
So you might think, oh, I'll just try this.
I'll do include *.ps1.
I don't see anything.
Well, that's because there's actually a little trick here with this,
and that's why you have to read the help.
Let me rerun the command.
So we're going to include .ps1, and you know what,
I'm going to also exclude that trial.ps1 that you see there.
For the include and exclude, you actually also have to Recurse,
and now that works.
So whether you use the filter, the path,
the name, the include, or exclude really all depends upon,
not only what you're trying to do, but maybe where you're trying to do it.
Let me give you another example here.
PowerShell has other drives other than the file system, you know,
things like the registry, and the functions,
and aliases.
So I'm going to just use the alias PSDrive.
It's the same command, Get‑ChildItem, and you can use the dir alias,
and I'm saying,
look at the alias PSDrive and find me all the aliases that start with c,
and I get an error message.
And in fact, it makes it very clear, Get‑ChildItem cannot call that method.
The provider does not support the use of filters.
What that means is that the alias provider doesn't support filtering.
Because filtering, there's a certain technical,
programmatic way that filtering is described,
and the filter parameter must use that,
and Get‑ChildItem can't work in the alias with that filter.
But that's fine because we have alternatives, right?
We could do ‑name.
That doesn't seem to work either.
But see, you've got to try these things.
Let's try the include trick.
I still have to use Recurse though.
Now that works, although something like this using the path might be even easier.
So there are a number of ways that you can filter using different parameters,
but you may have to experiment to find the right combination of
parameters to get you the results you want,
maybe with the least amount of work.
Now let me give you another example of where reading the help is very important.
So I'm going to run get‑ciminstance and get all of the
Win32_SystemDriver instances on my computer.
Alright, so I get a lot of good information, but it was more though than I want.
Really,
all I care about is I just want to see the ones where they're
running and the servicetype is the file system driver.
So get‑ciminstance has a filter parameter,
which is basically like a where clause in the SQL query.
Type this out here.
Alright,
So what I'm doing with the command here is
get‑ciminstances from that Win32_SystemDriver class,
and then the filter is basically,
get me everything where the state property is equal to running and the
servicetype is equal to file system driver. Query that I'm doing here,
this is a WQL query, a WMI type query, this uses the traditional
operators. Notice I'm using the equal sign.
I'm not using a PowerShell operator,
so the filter is generating a query that will limit kind of like a SQL
query and return back just the information that I want.
This becomes super important when you start scripting at scale.
If I were to run this command against 500 servers and bring the results back,
I want to have those 500 servers limit at the source and not
bring everything back to me, and then I filter. That's why early filtering is so important.