Hey everyone. Welcome back to the final part of my
course on putting PowerShell to work.
Hopefully you've seen how easy it is to find commands, figure out
what kinds of things they write to the PowerShell pipeline, and how
to manipulate and arrange PowerShell to get the results you want or
accomplish a given task.
Before I let you go though, we need to look at one more important concept,
and that is filtering.
Filtering is an important step in your PowerShell work and
something that you're going to do all the time,
even though you may not think of it as filtering.
When you run a command like Get‑Process,
you know you get a lot of results, but maybe you don't need to see everything.
Filtering is the process of removing items from the
pipeline that you don't need.
In other words,
you're telling PowerShell only show me these things that matter to me
right now. Depending on what you're doing, there are also significant
performance benefits when filtering.
Conversely,
the wrong kind of filtering, or not filtering at all, can slow you down.
Here's what I'm talking about. When we talk about filtering in PowerShell,
we really mean two kinds of filtering,
typically referred to as early filtering and late filtering.
Now these aren't commands, but rather concepts.
Early filtering is where you do the filtering at the source,
like a SQL query where you're asking for a specific set of
data based on some criteria. You don't pull down the entire
database, you only get what you need.
Late filtering is done at the destination. Now this would be the
equivalent of pulling down that entire database to your computer
and then selecting just what you want.
When using early filtering,
all you get are your desired or filtered objects. In late filtering,
your first command is getting everything,
and then you have to filter on what you truly need.
And of course there are performance implications to this. With early filtering,
especially if you're acquiring for something from the
network like a domain controller,
it's better to have the server do the work and only transmit
over the network what you really need.
As a general rule,
you don't do any late filtering until the initial
data is brought to your computer.
Now, that said,
don't feel that you should never use late filtering
techniques, sometimes that's your only option.
I want to make sure, though,
that you understand the different filtering philosophies.
I mean, in practical terms,
you might use both concepts in the same PowerShell expression.
But the bottom line is that you should always try to filter first or
as early as you can in your PowerShell expression.
You might hear people refer to this as the filter left or filter first practice.
This is something that you need to be in the habit of doing all the time.
It should just be part of your PowerShell muscle memory.
Late filtering, again, is not bad,
and there are plenty of times when that's exactly what you want to do.
It's only bad, per se, or if you will,
if there are early filtering techniques that you could
have or should have taken into account.
One approach to filtering is via parameters.
Consider these as early filtering techniques. Look for
parameters in the command you are using that will help you
limit the results, like filter, name, ID, and so on.
These are some of the parameters that you'll see in
a number of PowerShell commands.
However, don't assume that they all work the same in all commands.
You must read full command help and examples,
especially for that filter parameter.
That sounds like the perfect parameter,
right? But how you use that parameter is going to vary on the
command and where you might be using that command.
That's why you really need to read the help.
Where‑Object is the primary late filtering cmdlet.
You pipe a bunch of things to find things where the object meets some criteria.
The command has aliases of Where and the question mark, although I'm
not a big fan of that last one. I guess I just never made the
connection between question mark and Where.
But feel free to use either alias when working at a
PowerShell prompt. Where‑Object will compare the incoming
objects to some sort of filtering test.
If the object passes the test,
it is passed on into the pipeline to the next command.
Now even though Where‑Object is considered late filtering, I think you'll actually be using it often, so let me give you some details.