PowerShell Where-Object | Super Easy Object Filtering

Morning everyone! I wanted to revisit a root topic of mine: Windows PowerShell!

If you used the PowerShell Where-Object cmdlet, you know just how critical it is to learn. In my opinion, it’s one of the most versatile and widely used cmdlets in PowerShell.

So today, I wanted to give a more in-depth overview on what Where-Object is and how to use it properly.

Before we start, you can also read this on Medium.com!

What is PowerShell Where-Object?

Reading from the definition: The Windows PowerShell where-object takes in objects from the pipeline and filters them based on a condition specified in the script block.

What what does that actually mean?

I think it’ll be easier to show you graphically. Below is an image which shows Where-Object in it’s most basic form:

Where-Object { <condition> }
What is PowerShell Where-Object?

Where you have the input on the left, this would be another command inside PowerShell. Where-Object in the middle which takes in the output from the previous command. And finally the filtered output on the right.

Why Use PowerShell Where-Object?

You might have already seen that some PowerShell commands come with a -filter parameter already. A good example is the PowerShell Get-ChildItem cmdlet.

Now you might think: Well, why not just use the built in filter?

Well because it’s slower and older. Microsoft would have you think it’s faster, even having this on their website:

Filters are more efficient than other parameters. The provider applies filter when the cmdlet gets the objects rather than having PowerShell filter the objects after they’re retrieved. The filter string is passed to the .NET API to enumerate files

BUT PLEASE DON’T LISTEN TO THEM.

I did my owning testing after a job interview a couple years back, and found that using -Filter was far slower!

I’d recommend using Where-Object wherever you can. Mainly because of the performance benefits, but also because it’s easier to read!


How To Use PowerShell Where-Object?

Suppose you wanted to:

Find all the Windows processes that were running for Microsoft Edge?

Then you could use this simple and readable script:

Get-Process | Where-Object { $_.ProcessName -eq 'msedge' }

It’s a simple script but let me explain anyway with the below graphic:

How To Use PowerShell Where-Object? - get-process

Get-Process is actually returning every single process that is currently running.

The | symbol is called the pipeline. And this ‘pipes’ the output from Get-Process to the Where-Object cmdlet. Following this, we get the Get-Process output after it’s been filtered by Where-Object.

Maybe I’ve over explained it! But if you still don’t get it, read on or just start testing this by yourself. No better way to learn than to mess around and break stuff.

Other Examples

Say you want to get all the items that are 10MB or larger in your C:\Test folder:

Get-ChildItem -Path "C:\Test" | Where-Object { $_.Length -ge 10MB }

Or, you wanted to to see what processes are consuming more than 100 CPU seconds and using more than 200MB of memory:

Get-Process | Where-Object { $_.CPU -gt 100 -and $_.WorkingSet -gt 200MB }

Yeah I did kind of sneak that one in, but you can also combine multiple filtering conditions using -and or -or.

Tips for PowerShell Where-Object

There aren’t many tips that come to mind, apart from the performance difference mentioned above.

I’d also recommend you use the $_ notation sparingly as it is context dependant and represents the current pipeline value. This means that $_ could mean entirely different things throughout the run of your script.

It can also be tough to figure out what you can filter on, I’d recommend using the Get-Member cmdlet for that. You can pipe almost any cmdlet into it and get a full list of the properties to filter on. For example:

Get-ChildItem | Get-Member

FAQs

I hope you’ve learnt something about the Where-Object cmdlet today. Enjoy! 🎉

email popup image
Mark Harwood
NEVER miss a blog post again! Subscribe for email notifications whenever a new post is live!
Subscribe
NEVER miss a blog post again! Subscribe for email notifications whenever a new post is live!
Fill Out This Form, And I Will Be In Touch Shortly
Contact form image
I'll Be In Touch Soon