Where-Object

Filter input from the pipeline, control which objects will be passed along the command pipeline.
The '?' symbol and Where are both aliases for Where-Object.

Syntax
      Where-Object [-filterScript] {scriptblock}
             [-inputObject psobject] [CommonParameters]

Key
   -FilterScript scriptblock
       An expression that resolves to a Boolean (TRUE/FALSE) value.
       This will determine which input objects will be
       passed along the command pipeline.

   -inputObject psobject
       The objects to be filtered. Typically objects are
       passed to Where-Object through the pipeline.

   CommonParameters:
       -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
       -OutBuffer -OutVariable.

The '?' symbol and Where are both aliases for Where-Object and there is also a Where statement. If you explicitly want to run the Where-Object command, run Where-object or '?' .

Where-object determines which objects to pass along the pipeline by evaluating a script block that may include a reference to an object being filtered. If the result of the evaluation is True, the object being processed is passed along the pipeline; otherwise the object is discarded.

The scriptblock expression can use any of the PowerShell comparison operators (as long as the result is a boolean.)
Also: -not logical not (with ! as an alias) and -xor (Exclusive OR)

Examples

Get a list of files but exclude folders:

PS C:\> Get-ChildItem 'C:\Apps\' -Recurse | Where-Object {-not $_.PsIsContainer}

Get a list of all services that are currently stopped:

PS C:\> Get-Service | Where-Object {$_.Status -eq 'Stopped'}

Lists the processes with a working set greater than 25000K. (bytes = Kilobytes*1024):

PS C:\> Get-process | ? {$_.workingset -gt 25000*1024}

Get the processes with a ProcessName property that begins with the letter p. The -match operator enables you to use regular expressions:

PS C:\> Get-process | Where-Object { $_.ProcessName -match '^p.*' }

“The enemy of art is the absence of limitations” ~ Orson Welles

Related:

PowerShell Syntax - Regular Expressions
ForEach-Object - Loop for each object in the pipeline
Group-Object - Group the objects that contain the same value for a common property
Select-Object - Select objects based on parameters set in the Cmdlet command string
Sort-Object - Sort the input objects by property value
Where statement


© Copyright SS64.com 1999-2013
Some rights reserved