PowerShell Filter Operators [With Examples]

If you want to write good quality PowerShell script, then you should know how to use PowerShell filter operators. In this tutorial, I will show you how to use filter operators in PowerShell with examples.

PowerShell Filter Operators

PowerShell filter operators are used to compare values or filter elements of a collection against an input value.  Filter operators can be used with cmdlets like Where-Object or directly within the -Filter parameter of certain cmdlets, such as Get-ADUser.

There are several types of filter operators available in PowerShell, including:

  • Comparison Operators: Used to compare values and test conditions (e.g., -eq-ne-gt-lt-le-ge).
  • Containment Operators: Used to filter collections based on the presence or absence of a value (e.g., -contains-notcontains-in-notin).
  • Matching Operators: Used to match patterns or regular expressions (e.g., -like-notlike-match-notmatch).
  • Type Operators: Used to filter based on the type of objects (e.g., -is-isnot).

Let me show you each of these operator types with real examples.

Comparison Operators

Comparison operators are the most commonly used filter operators in PowerShell. They allow us to compare values and test conditions. Here are a few examples:

  • -eq (Equal to): Filters elements that are equal to the specified value.
Get-Process | Where-Object { $_.CPU -eq 10 }

This command retrieves all processes with a CPU usage of exactly 10.

  • -ne (Not equal to): Filters elements that are not equal to the specified value.
Get-Service | Where-Object { $_.Status -ne "Running" }

This command retrieves all services that are not in the “Running” state.

You can see the exact output in the screenshot below after I executed the above script using VS code.

PowerShell Filter Operators
  • -gt (Greater than): Filters elements that are greater than the specified value.
Get-ChildItem | Where-Object { $_.Length -gt 1MB }

This command retrieves all files with a size greater than 1 MB.

  • -lt (Less than): Filters elements that are less than the specified value.
Get-EventLog -LogName System | Where-Object { $_.EventID -lt 1000 }

This command retrieves all events from the System event log with an EventID less than 1000.

These are just a few examples of comparison operators in PowerShell. Other operators like -le (less than or equal to) and -ge (greater than or equal to) work similarly.

Check out PowerShell Match Operator Examples

Containment Operators

Containment operators in PowerShell are used to filter collections based on the presence or absence of a value. They are particularly useful when working with arrays or collections of objects. Let’s look at a couple of examples:

  • -contains: Filters a collection containing a specific value.
$numbers = 1, 2, 3, 4, 5
$numbers -contains 3

This example checks if the $numbers array contains the value 3, returning True.

  • -notcontains: Filters a collection that does not contain a specific value.
$fruits = "apple", "banana", "orange"
$fruits -notcontains "grape"

This example checks if the $fruits array does not contain the value “grape”, returning True.

You can see the output in the screenshot below:

Filter Operators in PowerShell

Matching Operators

Matching operators in PowerShell allow us to filter based on patterns or regular expressions. They are incredibly powerful when dealing with text-based data. Let’s explore a couple of examples:

  • -like: Filters elements that match a specified wildcard pattern.
Get-ChildItem -Path C:\Users\Bijay\Documents | Where-Object { $_.Name -like "Report*" }

This command retrieves all files in the specified directory that start with “Report”.

  • -match: Filters elements that match a specified regular expression pattern.
Get-Content -Path C:\Logs\ErrorLog.txt | Where-Object { $_ -match "\d{4}-\d{2}-\d{2}" }

This command retrieves all lines from the ErrorLog.txt file that contain a date in the format “YYYY-MM-DD”.

Matching operators in PowerShell provide flexibility in filtering based on patterns.

Type Operators

Type operators allow us to filter based on the type of objects. They are useful when dealing with a mix of object types in a collection. Here’s an example:

  • -is: Filters elements that are of a specified type.
Get-ChildItem | Where-Object { $_ -is [System.IO.DirectoryInfo] }

This command retrieves all directories (objects of type System.IO.DirectoryInfo) from the current location.

You can use the Type operators in PowerShell to narrow down results based on the specific type of objects we are interested in.

Use Filter Operators with Cmdlets

Many PowerShell cmdlets have built-in support for filter operators, allowing us to directly specify the filtering criteria without the need for a separate Where-Object cmdlet. Let me show you a few examples:

  • Get-ADUser: Retrieves user objects from Active Directory.
Get-ADUser -Filter "Department -eq 'IT'"

This command retrieves all user objects from Active Directory whose Department property is equal to “IT”.

  • Get-EventLog: Retrieves events from event logs.
Get-EventLog -LogName Application -Filter "EventID -eq 1000"

This PowerShell command retrieves all events from the Application event log with an EventID of 1000.

Combine Filter Operators

Filter operators in PowerShell can be combined using logical operators like -and-or, and -not to create more complex filtering conditions. This allows us to refine our results based on multiple criteria. Here’s an example:

Get-Process | Where-Object { ($_.CPU -gt 10) -and ($_.WorkingSet -gt 100MB) }

This command retrieves all processes with a CPU usage greater than 10, and a working set memory greater than 100 MB.

You can see the output in the screenshot below after I executed the above script.

PowerShell Filter Operators Examples

Conclusion

PowerShell filter operators allow us to refine and narrow down our results based on various criteria. In this tutorial, I have explained various filter operators available in PowerShell, including comparison operators, containment operators, matching operators, and type operators.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.