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> }
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!
Microsoft Clarity – The Definitive Guide
I know I’m late to discovering this tool, but for anyone else out that that…
Best Plugins For WordPress
If you’ve ever setup a WordPress website before, you’ll have probably seen the dizzying amount…
How To Turn Off iPhone 15
You might think you’re being dumb but trust me, I had to Google how to…
When Does The iPhone 16 Come Out?
Apple’s new iPhone, the iPhone 16, has just been released! Let’s go through everything you…
Blurry Dotted Background in Elementor
Been a while! Let’s kick things back into gear by showing you how I created…
How To Setup Subscriptions On Fiverr In 2024
If you have any gigs active on Fiverr, you might want to start generating regular…
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:
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! 🎉