3.4. Filtering Collections Using Where-Object

Very often, the built-in cmdlets provide more data than we need. Take Get-Process for example: it will return all processes and allow for rudimentary filtering by process name only. What if we want to get all processes that occupy more than 20MB of RAM? We have to pipe the output of Get-Process into Where-Object (I will use the shorthand where in this book. The other alias, ?, feels too cryptic and might inhibit readability). Can you guess the essential parameters that where needs? That's right—it takes a collection and a script block at minimum. The script block is executed against all objects and only the ones that evaluate to $true will be included in the resulting collection.

PS> Get-Process ...

Get Pro Windows PowerShell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.