PowerShell: How to Use Where-Object with Multiple Conditions


Often you may want to use the Where-Object cmdlet in PowerShell to filter an object based on multiple conditions

You can use the following methods to do so:

Method 1: Use Where-Object with -and Operator

$my_data | Where-Object { [int]$_.points -gt 20 -and [int]$_.assists -gt 7}

This particular example filters the $my_data object to only display the rows where the value in the points property is greater than 20 and the value in the assists property is greater than 7.

Method 2: Use Where-Object with -or Operator

$my_data | Where-Object { [int]$_.points -gt 30 -or [int]$_.assists -gt 10}

This particular example filters the $my_data object to only display the rows where the value in the points property is greater than 30 and the value in the assists property is greater than 10.

The following examples show how to use each method in practice with the following CSV file saved at the following location:

  • c:\users\bobbi\nba_data.csv

Here is what the contents of this file look like:

The file contains three columns that show the team, points and assists for various basketball players.

Example 1: How to Use Where-Object with -and Operator

We can use the following syntax to use the Where-Object cmdlet with the -and operator to filter the data from the CSV file to only show the rows where the value in the points property is greater than 20 and the value in the assists property is greater than 7:

$my_file = "c:\users\bobbi\nba_data.csv"

Import-Csv $my_file | Where-Object { [int]$_.points -gt 20 -and [int]$_.assists -gt 7}

The following screenshot shows how to use this syntax in practice:

PowerShell Where-Object with multiple conditions

Notice that only the rows shown in the output meet both of the following conditions:

  • The value in the points property is greater than 20.
  • The value in the assists property is greater than 7.

By using the -and operator within the Where-Object cmdlet, we specify that both conditions must be met for the row to be shown.

Example 2: How to Use Where-Object with -or Operator

We can use the following syntax to use the Where-Object cmdlet with the -or operator to filter the data from the CSV file to only show the rows where the value in the points property is greater than 30 or the value in the assists property is greater than 10:

$my_file = "c:\users\bobbi\nba_data.csv"

Import-Csv $my_file | Where-Object { [int]$_.points -gt 30 -or [int]$_.assists -gt 10}

The following screenshot shows how to use this syntax in practice:

PowerShell Where-Object with multiple conditions

Notice that each of the rows shown in the output meet at least one of the following conditions:

  • The value in the points property is greater than 30.
  • The value in the assists property is greater than 10.

By using the -or operator within the Where-Object cmdlet, we specify that at least one condition must be met for the row to be shown.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Return Multiple Values from Function
PowerShell: How to Find Duplicate Values in Array
PowerShell: How to Get Last Item in Array

Leave a Reply