Use Where-Object to Filter Results

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

You can always use Where-Object when you need to filter results. This cmdlet expects a filter script to determine the results you want. Here are some examples:
Try this to find only files greater than 1MB:

dir $env:windir | Where-Object { $_.Length -gt 1MB }

 

Use this when you need to find all processes with more than 20 seconds CPU time:

Get-Process | Where-Object { $_.CPU -gt 20 }

 

And to list all services not currently running, this is the code you will need:

Get-Service | Where-Object { $_.Status -eq 'Stopped' }

 

 

Twitter This Tip! ReTweet this Tip!


Posted Mar 15 2011, 08:00 AM by ps1
Copyright 2012 PowerShell.com. All rights reserved.