Home » Filtering Objects with Where-Object -Or in PowerShell

Filtering Objects with Where-Object -Or in PowerShell

In PowerShell, you can use the Where-Object cmdlet with a logical operator -or to combine multiple conditions.

The following methods show how you can do it with syntax.

Method 1: Filtering with Where-Object and -Or

 $employees = @(
  [PSCustomObject]@{ Name = "Alex"; Age = 30; Department = "IT" },
  [PSCustomObject]@{ Name = "Gary"; Age = 35; Department = "Sales" },
  [PSCustomObject]@{ Name = "Ricky"; Age = 38; Department = "IT" }
)

$result = $employees | Where-Object { $_.Age -gt 35 -or $_.Department -eq "IT"}

$result

This example will return the employees with age greater than “35” or who belong to the “IT” department.

Method 2: Filtering with Where-Object and -or for Strings

$languages = @("Python", "C#", "C", "C++", "VB", "Java", "Go")

$result = $languages | Where-Object { $_ -like "C" -or $_ -like "Go"}

$result

This example will return languages “C” or “Go“.

The following examples show how you can use these methods.

Filtering with Where-Object and -Or

Suppose you have an array of custom objects representing employees, and you want to filter for employees having age more than 35 years or belong to the IT department.

# Define an array of custom objects 
$employees = @(
  [PSCustomObject]@{ Name = "Alex"; Age = 30; Department = "IT" },
  [PSCustomObject]@{ Name = "Gary"; Age = 35; Department = "Sales" },
  [PSCustomObject]@{ Name = "Ricky"; Age = 38; Department = "IT" }
)

# Use Where-Object with the -or operator to filter the array
$result = $employees | Where-Object { $_.Age -gt 35 -or $_.Department -eq "IT"}

# Output the filtered result
$result

Output:

PowerShell Where-Object cmdlet with -Or operator
PowerShell Where-Object cmdlet with -Or operator

In this example, we define $employees array of custom objects with properties Name, Age, and Department.

We then pipe the $employees into the Where-Object cmdlet to filter the array to include objects where the Age property is greater than 35 or the Department property is IT and stores the result in $result.

Finally, we print the $result on the terminal that displays the filtered result.

Filtering with Where-Object and -or for Strings

Suppose you have an array of strings representing programming languages and want to filter for strings containing “C” or “Go“.

# define an array of strings
$languages = @("Python", "C#", "C", "C++", "VB", "Java", "Go")

# Use the Where-Object with the -or operator to filter strings
$result = $languages | Where-Object { $_ -like "C" -or $_ -like "Go"}

# Output the filtered result
$result

Output:

PowerShell Where-Object -or operator to filter strings
PowerShell Where-Object -or operator to filter strings

In this example, we define a $languages array that represents programming languages. We then pipe the $languages to the Where-Object cmdlet to filter for strings.

The Where-Object command uses the -or operator to specify multiple conditions, including programming languages containing “C” or “Go” and storing the result in $result.

Finally, we print the output on the terminal.

Conclusion

I hope the above article on filtering objects with the Where-Object cmdlet in PowerShell with the -or operator is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.