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 through the pipeline.

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

The '?' symbol and Where are both aliases for Where-Object. 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)

Comparison statement

A comparison statement, is a simplified syntax that can be used to filter the pipeline, neither the {brackets} or the pipeline placeholder $_ are required. Available in PowerShell 3.0 and greater.

Syntax
      command | Where test1 [conjunction test2] 

Key
   conjunction  Any of the following: (logical boolean operators) 
          -and, -or (Logical or), -bor (Bitwise or), -band (Bitwise and), -xor 

   Test1   An expression that resolves to a Boolean (TRUE/FALSE) value.

   Test2   An expression that resolves to a Boolean (TRUE/FALSE) value.
   comparison operators

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

For example - to list files on drive f: greater than 1000000 bytes:
PS C:\> Get-ChildItem f:\ | where Length -gt 1000000

Which is equivalent to:
PS C:\> Get-ChildItem f:\ | where {$_.Length -gt 1000000}

You can also read properties in a smilar way:
PS C:\> (Get-ChildItem f:\).Length

You can’t use this simplified syntax to set properties.

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 (method) - Filter input from a collection


© Copyright SS64.com 1999-2017
Some rights reserved