Powershell

Filter results with Where-Objects in PowerShell

The functions and cmdlets of PowerShell allow you to perform several tasks quickly and effectively. The Where-Object cmdlet of PowerShell filters the content that matches the condition specified in Where-object. The functionality of the Where-Object cmdlet is extended by practicing the supported list of parameters. This article focuses on providing a brief demonstration of filtering the content with Where-Object.

How to filter results using Where-Object

This section contains the syntax and the basic functioning mechanism that assist in filtering results using Where-Object. The Where-Object works on the grounds of one of the following syntaxes:

> <PSObject> | Where-object <parameters>

> Where-Object -InputObject <PSObject> -Property <name> -Value <value-name>

InputObject: This parameter gets the object name that you want to use (for filtering results) with Where-Object.

Property: Refers to the name of the property of an object

The objects can be piped with the Where-Object cmdlet and each result is filtered based on the following parameters.

Value: This parameter filters inside the property’s name and for that one of the following parameters of Value can be exercised:

The comparison operators are useful in using the Where object and all these operators are used to compare property value with value mentioned as a specified value. The parameter check if the property value is:

  • EQ: same as specified in the command
  • GT: greater than the specified/filter value
  • NE: different from the specified/filter value
  • LT: less than the specified/filter value
  • GE: greater than or equal (GE) to the specified/filter value
  • LE: less than or equal to the specified/filter value
  • Contains: This parameter Looks for the exact match of the property value inside the collection of objects.
  • In: Prints the content where the specified value is included in the property value
  • Like: The wild card character is accepted in a specified value and this parameter matches the property value with the specified value.
  • Match: Those results are printed where the property value matches the specified regular expression

Apart from these, each parameter (other than comparison parameters) has its reciprocal and is exercised by using Not in the start. For instance, NotContains, NotIn, NotLike, and so on.

Note: All the parameters supported by the value described here are case-insensitive. However, prefixing “C” before each operator makes it case-sensitive. For example, CContains, CLike, CEQ, CNotIn, and so on.

How to filter results using the Where-object cmdlet in PowerShell

The results are filtered using the parameters supported by -Value. You will find a list of examples that would be productive for you to understand how the results are filtered using Where-Object.

Example 1: Using Comparison operators to filter results

The Get-Command cmdlet of PowerShell prints the list of cmdlets, functions, and aliases supported. This example makes use of the way to get the desired result using the comparison operators supported by the Where-Object cmdlet.

The following command will filter the Alias name from the CommandType property of Where-object.

> Get-Command | Where-Object -Property CommandType -EQ Alias

Text Description automatically generated

The amount of processor time(in seconds) each process has used on all processors is referred to as CPU(s) in the Get-Process cmdlet. The following command practices Where-Object with Get-Process to get only those processes that have CPU(s) greater than or equal to 500s.

> Get-Process | Where-Object -Property CPU -GE 500

Text Description automatically generated with medium confidence

One can use multiple comparison operators to filter the result to get more filtered content. For instance, the command written below exercises the “LT” and “LE” operators to filter the content where CPU is less than 100 or the working set is less than or equal to 150.

> Get-Process | Where-Object {($_.CPU -LT 100.0) -OR ($_.WorkingSet -LE 150.0)}

Text Description automatically generated with medium confidence

Example 2: Using Containment and Matching operators

The containment operators are Contains, IN and their reciprocals. Whereas the Like, Match and their reciprocals are referred to as matching operators. This example provides a few commands that practice containment and matching operators to filter the content using the Where-Object cmdlet.

The following command looks for containment of the Acrobat process in the ProcessName field of processes:

> Get-Process | Where-Object -Property ProcessName -Contains Acrobat

A screenshot of a computer Description automatically generated with medium confidence

There is a matching operator named Match that filters the result on a broader aspect. For instance, the Contains operator only prints fields that have an exact match of the specified value. However, the Match operator would give you the results where the specified value is contained in the property value of the object. For instance, the command stated below provides the result that has Acrobat (partially/completely) as a process name in the ProcessName column.

> Get-Process | Where-Object -Property ProcessName -Match Acrobat

Text Description automatically generated

In the above-stated examples, the operators are practicing in a case-insensitive environment. If the operators are used with “C” as a prefix, then they would act as case-sensitive. The following command will get you to those fields that match the “acrobat” by keeping the case sensitivity in focus.

> Get-Process | Where-Object -Property ProcessName -CMatch acrobat

A picture containing logo Description automatically generated

Conclusion

PowerShell Where-object cmdlet filters the objects based on some criteria specified in a parameter. This post describes the working flow and implementation of the Where-Object cmdlet to filter results. You would get a brief introduction and working flow of the Where-Object cmdlet. Moreover, we have provided several examples that teach you to filter results using Where-Object. It is concluded that Where-Object functionality strongly depends on the parameters supported by it. With the help of this parameter, results are filtered rigorously based on the property values and their comparison with the specified value.

About the author

Adnan Shabbir