Powershell

PowerShell Where-Object clause

The Where-Object clause in PowerShell filters the content according to a specific value of the property of an object. The Where-Object clause can also be used with any cmdlet/function to target a specific property value of an object. This article provides a brief demonstration of the Where-Object clause in PowerShell. After following this guide, you would be able to get good hands-on where-object clauses. So, let’s start with this guide.

How the Where-Object clause works

As discussed earlier, the Where-Object clause can be associated with any cmdlet/function. The Where-Object clause follows two formatting criteria to perform the action. You may adopt any of the mentioned below.

The first syntax executes the Where-Object clause in a script block format whereas the second syntax performs the action by using a comparison statement.

Note: The script block comprises multiple expressions/statements to perform the action as a single unit (enclosed in braces). However, the comparison statement is written in normal English.

Script block syntax: Where-Object {$_.<Property-Name> <Parameters> "<Value>"}

Comparison statement syntax: Where-Object <Property-Name> <Parameters> "<Value>"

It is observed that the basic terms in both syntaxes are the same and are defined below:

<Property-Name>: This refers to the property name of the object collection.

<Parameters>: Where-Object supports multiple parameters that bridge the relationship between property and the value.

<Value>: This option refers to the value of property.

Among these instances of syntax, the parameters are the main ingredient. So, let’s have a quick look at the parameters that can be used in the Where-Object clause.

Parameters supported by Where-Object clause

The parameters supported by the Where-Object clause are mostly the comparison operators and are defined below:

  • EQ: Returns the collection of objects that equals the specified value in the command.
  • NE: This parameter shows the objects that do not match the value.
  • LT: Those objects are printed that satisfy the less than condition.
  • LE: Checks the collection for less than or equals condition.
  • GT: Looks for objects that have greater than the specified value.
  • GE: The greater than or equal operator compares the object values for the specified value.
  • Not: This refers to the property that does not exist or the value is null.
  • Match: This expression matches the regular expression.

How to use PowerShell Where-Object clause

This section practices the use of the Where-Object clause in PowerShell. Several examples are quoted to convey the message in an understandable manner.

Example 1: Using Where-Object clause on Get-Command cmdlet

The Get-Command cmdlet lists down all the Alias, Cmdlets, functions supported by PowerShell. Let’s say we want to get the Function provided by this command. To do so, the command with the following properties is executed in the PowerShell console:

  • Firstly, the Get-Command is piped with the Where-Object clause
  • Then a comparison operator(-EQ) is used to find the CommandType(Property) values equal to Function.

Resultantly, the command will print the desired values that satisfy the above mentioned condition.

> Get-Command | Where-Object CommandType -EQ Function

It is observed that the command is executed in a comparison statement format. Moreover, the same command can be executed using the script block syntax in the following way.

> Get-Command | Where-Object {$_.CommandType -EQ "Function"}

Text Description automatically generated with low confidence

Example 2: Using Where-Object clause on Get-Process cmdlet

Let’s perform the Where-object clause on the Get-Process cmdlet. The Get-Process cmdlet prints the processes of the system. The command provided below prints only those processes that have ProcessName equal to chrome. For this, -EQ parameter is used, the property name is ProcessName and the value of the property is chrome. So, after formatting, the command is written below:

> Get-Process | Where-Object ProcessName -EQ chrome

Table, calendar Description automatically generated

The equivalent script block command is written below. Both commands produce the same output but the syntax is different.

> Get-Process | Where-Object {$_.ProcessName -EQ "chrome"}

Table, calendar Description automatically generated

Example 3: Using Where-Object clause with Get-Service cmdlet

The Get-Service cmdlet prints the services of your windows machine in the shell. You can retrieve the status of the services using the Where-Object class. For instance, if a list of only stopped services is required then the Where-Object is used in the following way.

> Get-Service | Where-Object Status -EQ Stopped

Text Description automatically generated

Conclusion

The Where-Object clause in PowerShell allows considering those objects that satisfy the condition specified. For this, the comparison operators are used to get the objects that match the values of a property. In this demonstration, the working mechanism and usage of the Where-Object clause are discussed in detail. The main functionality of the Where-Object clause depends on the comparison parameters. These operators check the values of the objects and compare them with the specified values. Moreover, you would find a detailed description of each parameter as well that can be used with the Where-Object clause.

About the author

Adnan Shabbir