header image

Select-Object or Where-Object

Posted by: | August 11, 2014 | No Comment |

Both Select-Object and Where-Object (referred to by their aliases of select and where from now on) are both used to filter data.


It is important to know the way these 2 cmdlets are used.


Where is used to restrict the objects on the pipeline to those where one or more properties satisfy the filter criteria e.g.

Get-Process | where CPU -gt 20


You get a reminder of this if you you use the full syntax

Get-Process | where -FilterScript {$_.CPU -gt 20}


As a matter of style you very rarely see anyone using the parameter name –FilterScript.


Select is used to cut the number of properties on an object to just those you want to work with e.g.

Get-Process | select Name, Id, CPU


If you want just those properties for the processes where CPU time is greater than 20 seconds you need to combine them on the pipeline:

Get-Process | where CPU -gt 20 | select Name, Id, CPU


There isn’t a way to embed a where type filter in a select or vice versa. Keep it simple. Use the pipeline and let the cmdlets do the job for which they were designed.

under: PowerShell Basics