PowerShell for Windows Admins

Aug 11 2014   1:44PM GMT

Select-Object or Where-Object

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: