PowerTip: Use PowerShell 4.0 Where Operator to Filter


Summary: Use Windows PowerShell 4.0 Where operator to filter results.

Hey, Scripting Guy! Question How can I use a Where operator in Windows PowerShell (without using the pipeline or Where-Object)
           to filter through processes?

Hey, Scripting Guy! Answer Use Get-Process  to group the processes into a collection, then use the Where operator
           and filter on the process name, for example:

(Get-process).Where({$_.ProcessName -eq 'winlogon'})

Comments (3)

  1. Chen V says:

    This is interesting in PowerShell 4.0 and you can adopt select-Object as well

    (65..90).Where({$_ -gt 80} , ‘Last’ , 3)

    Where({Expression} , ‘Mode’ , Value)

  2. Kirk "Poshoholic" Munro says:

    The Where method is a *lot* more feature rich than simple filtering. You can read all about it here:
    http://www.powershellmagazine.com/2014/10/22/foreach-and-where-magic-methods/

  3. ed wilson says:

    @Poshoholic you are right, the where method is actually very powerful, and cool too.

Skip to main content