Powershell

PowerShell Filter

PowerShell is an object-based structure command-line shell. It deals with commands that are called cmdlets. PowerShell also has a feature to filter out the results using PowerShell Where-Object filter cmdlet. Comparison operators are added as a parameter in the Where-Object cmdlet, which helps it to filter results by comparing the values based on the added condition.

In this tutorial, we will discuss PowerShell Filters in detail. Let’s start!

How to filter results using PowerShell?

In PowerShell, the Where-Object command filters out or narrows down the results with the help of the added comparison operator.

Syntax
To filter any result, use the below syntax of Where-Object piped with PowerShell Object:

> <PS-Object> | Where-Object [-Property] <name> [-Filter] <comparisonoperator> [-FilterValue] <value-name>

PowerShell Comparison Operators

Here, we have enlisted some of the majorly used comparison operators with the Where-Object cmdlet:

Filter Operator Description
-eq Equals to
-ne Not equals tp
-contain Contains the particular value
-ge Greater than equals to
-le Less than equals to
-gt Greater than
-lt Less than
-match Match with the particular value

Have a look at some of the examples of filtering results using Where-Object PowerShell command.

Example 1: PowerShell filter with -eq operator

In this example, we will filter out the services by using the -eq (equals to) operator:

> Get-Service | Where-Object -Property Status -eq Stopped

The Get-Service command will fetch the list of the services and piped it as an input to the Where-Object command, which will then filter out the services whose status equals Stopped:

Example 2: PowerShell filter with -contain operator

We will now utilize the -contain operator as a parameter in the Where-Object command to list out the processes containing the ProcessName as chrome:

> Get-Process |Where-Object -Property Name -contain chrome

The Get-Process command will retrieve the list of all processes and piped it to the Where-Object command for further processing that is stated above:

Example 3: PowerShell filter with -match operator

In the below-given command, we will use the -match operator with the Where-Object command:

> Get-Service |Where-Object -Property DisplayName -match "Application"

The output will display the list of all the services with having the word Application in their DisplayName:

That’s all! We have compiled all of the related information about filters in PowerShell with examples.

Conclusion

To filter out the results of your choice, you can use PowerShell filter Where-Object cmdlet. Its syntax is given as: <PS-Object> | Where-Object [-Property] <name> [-Filter] <comparisonoperator> [-FilterValue] <value-name>. The Where-Object command filters out results based on the condition using comparison operators, and its output depends on the specified parameters. This tutorial discussed PowerShell filters with appropriate examples.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.