PowerShell Genie

Where-Object

Use Where-Object to select objects from a collection based on their property values. See Microsoft.PowerShell.Utility module requirements and examples.

Command 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — Where-Object

Syntax

1PS> Where-Object [-FilterScript] <ScriptBlock> [<CommonParameters>] Where-Object [-Property] <String> [[-Value] <Object>] [-EQ | -NE | -GT | -GE | -LT | -LE | -Like | -NotLike | -Match | -NotMatch | -Contains | -NotContains | -In | -NotIn | -Is | -IsNot] [<CommonParameters>]
See the command anatomy

Inspect this authored example with fictional data in the bounded browser model. The numbered stages show how its objects change.

Get-Service | Where-Object Status -eq Stopped
  1. Stage 1Get-ServicePositional values or no named parameters
  2. Stage 2Where-Object-eq

    The object workshop

    Watch your command work.

    Change the command. Step through the pipeline. Inspect what survives.

    5 fictional services

    Browser simulation · fictional services · Ctrl+Enter to run

      Object properties

      Enable JavaScript to step through this example. The lessons and command reference remain available.

      Your task: return the stopped service, with exactly Name and Status.

      Try it

      Windows PowerShell

      PowerShell example · browser support is limited.

      Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.

      Interactive simulation · fictional data

      Examples

      Filter by Status

      1PS> Get-Service | Where-Object Status -eq Stopped
      Show reference output

      Illustrative output · values vary by environment

      Name   Status   StartType  DisplayName              
      -----  -------  ---------  -------------------------
      WinRM  Stopped  Manual     Windows Remote Management

      Filter by CPU

      1PS> Get-Process | Where-Object CPU -gt 100
      Show reference output

      Illustrative output · values vary by environment

      Name     ProcessName  Id    CPU     WS         WorkingSet  WorkingSet64
      -------  -----------  ----  ------  ---------  ----------  ------------
      chrome   chrome       9184  842.31  486539264  486539264   486539264   
      MsMpEng  MsMpEng      2600  950     220200960  220200960   220200960   

      Filter by CPU and Id

      1PS> Get-Process | Where-Object { $_.CPU -gt 100 -and $_.Id -gt 1000 }
      Show reference output

      Illustrative output · values vary by environment

      Name     ProcessName  Id    CPU     WS       
      -------  -----------  ----  ------  ---------
      chrome   chrome       9184  842.31  486539264
      MsMpEng  MsMpEng      2600  950     220200960

      Parameters

      Parameter definitions:

      • -FilterScript
      • -Property
      • -Value
      • -EQ

      Where-Object on Microsoft Learn

      Practical use and troubleshooting

      Practical scenario: Use Where-Object when you need to select objects from a collection based on their property values. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.

      Common error and fix: An empty result may come from filtering a property that was already removed. Inspect Get-Member and apply the filter before a Select-Object projection that drops the required property. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.

      See also: Select-Object. Where-Object keeps matching input objects; Select-Object chooses properties or a subset of objects. Projection is different from testing whether an object satisfies a condition. Read the linked command before substituting it; similar names do not imply identical inputs or output.

      Where-Object reference on Microsoft Learn

      Put this command to work

      Filter stopped services

      Return the complete records for stopped services.

      Review the lesson

      Recap

      Where-Object Use Where-Object to select objects from a collection based on their property values. See Microsoft.PowerShell.Utility module requirements and examples.

      Next: ForEach-Object
      Genie guideWhere-Object

      Use Where-Object to select objects from a collection based on their property values. The console below is pre-loaded — run it, then change a value and run it again.