7.1. Using the where-object Cmdlet

The where-object cmdlet filters the objects presented to it. Most commonly, when you use the where-object cmdlet on the command line, the objects it filters will come from an earlier step in a pipeline.

In a standard install, you are likely to have two aliases available to use in place of the full form of where-object: where and ?. To find the aliases available for the where-object cmdlet on your system use this command:

get-childitem alias:\ |
where-object -filterScript{$_.Definition -match "where"}

or:

get-childitem alias:\ |
where-object {$_.Definition -match "where"}

The -filterScript parameter is used to filter objects. As the name of the parameter suggests, its value is a script. The script is enclosed in paired curly braces. The -filterScript parameter is a positional parameter in position 1.

7.1.1. Simple Filtering

The argument to the where-object cmdlet is a Windows PowerShell expression that is the value of the -filterScript parameter, and it returns a boolean value. The expression is contained in paired curly brackets and uses a number of operators, which I describe later in this section.

You might want to find out which services on a machine are running. Of course, you can do that using the Services Microsoft Management Console snapin, but you can also do it easily from the Windows PowerShell command line. The get-service cmdlet finds all services installed on a machine. This example uses the where-object cmdlet to display information ...

Get Professional Windows® PowerShell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.