Powershell

How to Use Multiple Conditions in PowerShell Where-Object?

In PowerShell, the “Where-Object” cmdlet is used to filter out the objects or values from an object. The Where-Object filter comprises single or multiple conditions. The multiple-condition filter allows you to filter out the items and return only a few that match these conditions.

To apply Where-Object, it is recommended to pipe it with other PowerShell cmdlets. Moreover, multiple conditions can be defined using script blocks, various operators, and so on.

This post will demonstrate the working and usage of multiple conditions in PowerShell Where-Object.

How to Use Multiple Conditions in PowerShell “Where-Object”?

PowerShell provides the following three possibilities to use multiple conditions in PowerShell:

  • Using script blocks and equality operators.
  • Using script blocks and containment operators.
  • Using script blocks, equality, and matching operators.

Method 1: Use Multiple Conditions in PowerShell “Where-Object” With Script Blocks and Equality Operators

A script block comprises a set of multiple statements enclosed in curly braces. These types of blocks can be joined with the Where-Object cmdlet using the “-FilterScript” parameter.

Example

In the following example, we will exercise equality operators with script blocks to use multiple conditions in PowerShell.

Let’s understand how a script block is created using the below-mentioned syntax:

> {$_.PropertyName -matching-parameter <value>}

In the above syntax, the “$_.” is the symbol used to refer to a property. The property and its value (to be searched) are joined using any equality operator (-gt, -ge, -lt, -le, -eq).

Here, we will use a script block to filter the processes based on the CPU usage of the process and NPM (Non-Paged Memory used by a process). The processes fulfilling the following conditions are filtered using the script block:

  • Having NPM greater than or equal to 30
  • Having CPU usage greater than 3000:
> Get-Process | Where-Object -FilterScript {($_.NPM -lt 30) -and ($_.CPU -gt 300)}

The output shows that only the processes that have NPM greater than

Method 2: Use Multiple Conditions in PowerShell “Where-Object” With Script Blocks and Containment Operators

Containment operators are the types of comparison operators. These operators are easy to understand and read as compared to the script blocks in PowerShell.

The list of containment operators and their suitable descriptions is listed in the following table:

Operator Description
“-contains” The value on the right side of the operator is present on the group of values on the left side.
“-ccontains” The right operand is present in the group of values present on the right. The -ccontains is case sensitive.
“-notcontains” The value on the right side of the operator is not present on the left side.
“-cnotcontains” This operator works the same as that of the -notcontains but with case sensitivity.

Example

> Get-Service | Where-Object {($_.StartType -in 'Manual') -and ($_.Status -notcontains 'Running')}

According to the above code:

  • ($_.StartType -in ‘Manual’)” fetch the services which are in “Manual” mode.
  • -and” is used as a concatenator to combine both conditions.
  • ($_.Status -notcontains ‘Running’)” fetch the services which are not running, which means the services that are stopped:

Method 3: Use Multiple Conditions in PowerShell “Where-Object” With Using Equality and Matching Operators

Matching operators are used to match a specified condition or pattern of elements. These operators include “-match”, “-notmatch”, “-like”, and “-notlike”.

Example

This command contains multiple conditions based on equality and matching operators:

> Get-Command | Where-Object {($_.CommandType -eq 'cmdlet') -and ($_.Name -like '*import*')}

According to this example:

  • ($_.CommandType -eq ‘cmdlet’)” get the commands equal to “cmdlet” using the “-eq” operator.
  • ($_.Name -like ‘*import*’)” get the names starting with “Import” using the “-like” operator:

That was all about using multiple conditions in PowerShell.

Conclusion

Multiple conditions are the ones in which more than two statements or conditions are defined. These conditions are used in PowerShell “Where-Object” with the combination of Script Blocks and Comparison statements. This post has provided a complete guide to use multiple conditions in PowerShell.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.