PowerShell where-object multiple conditions [With 8 Examples]

Do you want to know how to use multiple conditions in PowerShell Where-Object? In this tutorial, I will explain everything with examples of “PowerShell where-object multiple conditions“.

PowerShell Where-Object for Multiple Conditions

PowerShell Where-Object cmdlet is used in a pipeline to filter out objects that do not match the specified criteria. You can use it with any cmdlet that outputs objects, filtering based on properties of those objects.

Here’s a simple example:

Get-Process | Where-Object { $_.CPU -gt 10 }

This command gets a list of all processes and pipes it to Where-Object, which filters out processes using more than 10 CPU seconds.

Now, let’s move on to using multiple conditions in the PowerShell Where-Object cmdlet. You can combine conditions using the -and and -or logical operators within the script block {} of Where-Object.

Using -and

The -and operator allows you to specify that all conditions must be true for the output to include the object. For instance, if you want to find processes that are using more than 10 CPU seconds and have a working set size greater than 50MB, you can use the following command:

Get-Process | Where-Object { $_.CPU -gt 10 -and $_.WS -gt 50MB }

Using -or

Conversely, the -or operator means that if any condition is true, the object will be included in the output. So, if you’re looking for processes that are either using more than 10 CPU seconds or have a working set size greater than 50MB, you’d use this command:

Get-Process | Where-Object { $_.CPU -gt 10 -or $_.WS -gt 50MB }

Combining -and and -or

You can also combine these operators to create more complex queries. For example, if you’re looking for processes that are using more than 10 CPU seconds and have a working set size greater than 50MB or processes that are using less than 5 CPU seconds, you can use parentheses to group conditions:

Get-Process | Where-Object { ($_.CPU -gt 10 -and $_.WS -gt 50MB) -or $_.CPU -lt 5 }

Here you can see the output after I executed the PowerShell script using VS code.

PowerShell where-object multiple conditions

PowerShell where-object multiple conditions Examples

Let’s go through some practical examples to see PowerShell Where-Object with multiple conditions in action.

Example 1: Finding Specific Services

Suppose you want to find services that are either stopped and can be paused or are running and cannot be paused. You would use the following command:

Get-Service | Where-Object { ($_.Status -eq 'Stopped' -and $_.CanPauseAndContinue -eq $true) -or ($_.Status -eq 'Running' -and $_.CanPauseAndContinue -eq $false) }

Example 2: Filtering Files

If you need to find files in a directory that are either larger than 1MB or have been modified in the last 7 days, you can use this command:

Get-ChildItem | Where-Object { $_.Length -gt 1MB -or $_.LastWriteTime -gt (Get-Date).AddDays(-7) }

Example 3: Network Information

To display network adapters that are either disconnected or have a speed less than 1 Gbps, you would use:

Get-NetAdapter | Where-Object { $_.Status -eq 'Disconnected' -or $_.Speed -lt 1GB }

Example 4: Filtering Custom Objects

Imagine you have an array of custom objects representing employees, and you want to find employees in a certain department who have been with the company for more than five years or have a salary greater than $60,000. Here’s how you might define the array and use Where-Object:

$employees = @(
    [PSCustomObject]@{Name='John Doe'; Department='Sales'; YearsEmployed=6; Salary=55000},
    [PSCustomObject]@{Name='Jane Smith'; Department='IT'; YearsEmployed=8; Salary=70000},
    [PSCustomObject]@{Name='Emily Johnson'; Department='Marketing'; YearsEmployed=3; Salary=45000},
    [PSCustomObject]@{Name='Michael Brown'; Department='Sales'; YearsEmployed=10; Salary=65000}
)

$filteredEmployees = $employees | Where-Object { ($_.Department -eq 'Sales' -and $_.YearsEmployed -gt 5) -or $_.Salary -gt 60000 }
$filteredEmployees

This command filters the array of employees to return John Doe and Michael Brown from the Sales department with more than five years of employment, and Jane Smith from the IT department due to her high salary.

Once you execute the above PowerShell script, you can see the output in the screenshot below.

PowerShell where-object multiple conditions Examples

Example 5: Processing Arrays of Numbers

Let’s say you have an array of numbers, and you want to find numbers that are either even and greater than 50 or odd and less than 20. Here’s how you could do it:

$numbers = 1..100
$filteredNumbers = $numbers | Where-Object { ($_ % 2 -eq 0 -and $_ -gt 50) -or ($_ % 2 -ne 0 -and $_ -lt 20) }
$filteredNumbers

This command filters the array of numbers to return those that meet the specified conditions.

Example 6: Filtering Arrays of HashTables

Suppose you have an array of hash tables representing different types of devices in a network, and you want to find devices that are either routers or have an IP address starting with “192.168”:

$devices = @(
    @{Type='Router'; IPAddress='10.0.0.1'},
    @{Type='Switch'; IPAddress='192.168.1.2'},
    @{Type='Router'; IPAddress='192.168.0.1'},
    @{Type='Firewall'; IPAddress='172.16.0.1'}
)

$filteredDevices = $devices | Where-Object { $_.Type -eq 'Router' -or $_.IPAddress -like '192.168.*' }
$filteredDevices

This command returns the devices that are routers or have IP addresses starting with “192.168”.

Example 7: Advanced Filtering with Custom Properties

You can also use calculated properties in Where-Object to filter based on a combination of object properties. For instance, if you want to find files that are either read-only or hidden and larger than 1MB, you could do the following:

Get-ChildItem | Where-Object { ($_.Attributes -match 'ReadOnly' -or $_.Attributes -match 'Hidden') -and $_.Length -gt 1MB }

This command filters files in the current directory to return those that are read-only or hidden and larger than 1MB.

Example 8: Nested Objects Filtering

If you’re dealing with nested objects such as JSON data, you may need to filter based on properties of nested objects:

$jsonData = @(
    [PSCustomObject]@{Name='Server1'; Details=@{OS='Windows'; RAM=16GB}},
    [PSCustomObject]@{Name='Server2'; Details=@{OS='Linux'; RAM=32GB}},
    [PSCustomObject]@{Name='Server3'; Details=@{OS='Windows'; RAM=8GB}}
)

$filteredServers = $jsonData | Where-Object { $_.Details.OS -eq 'Windows' -and $_.Details.RAM -ge 16GB }
$filteredServers

This command filters the array of server objects to return those running Windows with at least 16GB of RAM.

By using Where-Object with multiple conditions, you can effectively filter complex data structures in PowerShell, whether you’re dealing with custom objects, arrays, hash tables, or nested objects. The key is to understand the properties of the objects you’re working with and construct your conditions to target the data you need accurately.

Conclusion

In this PowerShell tutorial, I have explained how to use multiple conditions in PowerShell Where-Object and also I have provides a few examples of “PowerShell where-object multiple conditions“.

You may also like: