Search notes:

PowerShell cmdLet Where-Object

The where-object cmdLet allows to specify a condition that determines which objects are kept in the pipeline.
Object that don't satisfy the specified conditions are discarded from the pipeline, that is, they are not passed to the next cmdLet in the pipeline.
Thus, the where-object cmdLet resembles the SQL where clause. Accordingly, an alias for where-object is where.
Note, this where must not be confused with where.exe which returns the directories in which executables are found.

Ways to construct a where-object command

There are two methods to construct a where-object command:

Example

The get-command cmdLet allows to find cmdLets whose verb is format with is -verb argument:
PS C:\> get-command -verb format

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Format-Hex                                         3.1.0.0    Microsoft.PowerShell.Utility
Function        Format-Volume                                      2.0.0.0    Storage
Cmdlet          Format-Custom                                      3.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-List                                        3.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-SecureBootUEFI                              2.0.0.0    SecureBoot
Cmdlet          Format-Table                                       3.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-Wide                                        3.1.0.0    Microsoft.PowerShell.Utility
However, I also get the commands whose commandType is Function which I don't want to see. I can exclude them by pipelining them into the where-object:
PS C:\> get-command -verb format | where-object commandType -eq cmdLet

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Format-Custom                                      3.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-List                                        3.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-SecureBootUEFI                              2.0.0.0    SecureBoot
Cmdlet          Format-Table                                       3.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-Wide                                        3.1.0.0    Microsoft.PowerShell.Utility
I am much happier now with the result. But still, I want to get rid of the format-secureBootUEFI cmdLet because it does not fit in the list. So I need to add a second criteria.
It seems that PowerShell is not capable of specifying multiple criterias without script block. So, I use a script block for the second criteria.
PS C:\> get-command -verb format | where-object { $_.commandType -eq 'cmdLet' -and $_.source -match 'PowerShell' }

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Format-Custom                                      3.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-List                                        3.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-Table                                       3.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-Wide                                        3.1.0.0    Microsoft.PowerShell.Utility
Note: when using script blocks, the object on which the criterias are performed needs to be referenced by the automatic variable $_.

Removing elements from an array

The following example removes elements from an array that contains numbers. Elements that contain numbers are matched by the regular expression \d whose meaning is reversed with the -notMatch operator.
'foo','bar', 'hello world', 'the number is 42.', 'baz' | where-object {$_  -notMatch '\d' }

Abbreviation with the question mark

where-object can also be abbreviated with a question mark:
get-service | ? name -match xbox

See also

Powershell command noun: object
Common first class function: filter
Applying a comparison operator on an array.
where-object has a non-approved verb

Index