PowerShell Where-Object Does Not Match [With Examples]

PowerShell Where-Object cmdlet allows users to filter objects based on their properties and values. In this PowerShell tutorial, we’re going to delve into the -NotMatch parameter of the Where-Object cmdlet in PowerShell and how it can filter objects that do not match a specified pattern.

PowerShell Where-Object Does Not Match

The Where-Object cmdlet in PowerShell is used to filter objects from a collection based on their property values. It’s akin to the SQL WHERE clause and is often used with pipeline input. The cmdlet takes a script block as an argument, which contains the condition to be evaluated for each object. If the condition evaluates to true, the object is passed down the pipeline; otherwise, it is discarded.

The -NotMatch Operator

In PowerShell, -match and -notmatch are operators used with regular expressions (regex) to match strings against patterns. The -match operator returns true when the input matches the regex pattern, while -notmatch returns true when the input does not match the pattern.

For example, if you want to filter a list of file names to exclude those that end with .log, you can use -notmatch with the Where-Object cmdlet as follows:

Get-ChildItem | Where-Object { $_.Name -notmatch '\.log$' }

This command gets all items in the current directory, but only passes through those whose names do not end with .log.

PowerShell Where-Object Does Not Match Examples

Let’s look at some practical examples of using Where-Object with the -NotMatch operator in PowerShell.

Example 1: Filtering Processes

Suppose you want to get a list of all running processes that are not instances of Chrome:

Get-Process | Where-Object { $_.ProcessName -notmatch 'chrome' }

This command returns all processes where the process name does not match “chrome”.

You can see the output in the screenshot below:

PowerShell Where-Object Does Not Match

Example 2: Excluding Specific Text from Content

Imagine you have a text file with multiple lines, and you want to read the lines that do not contain the word “error”:

Get-Content .\logfile.txt | Where-Object { $_ -notmatch 'error' }

This command reads ‘logfile.txt’ and outputs lines that do not contain “error”.

Example 3: Filtering Objects with Multiple Conditions

You can also combine -notmatch with other operators to create more complex filters. For instance, if you want to filter out services that are stopped and whose names do not match “win”:

Get-Service | Where-Object { $_.Status -ne 'Running' -and $_.Name -notmatch 'win' }

This command filters for services that are not running and whose names do not contain “win”.

When using PowerShell -NotMatch in Where-Object, it’s important to follow some best practices:

  1. Case Sensitivity: By default, -match and -notmatch are case-insensitive in PowerShell. If you require case sensitivity, you can use the c prefix, as in -cnotmatch.
  2. Performance: Filtering with Where-Object can be slow on large collections. When possible, use cmdlet-specific filtering parameters which are usually more efficient.
  3. Complex Patterns: For complex patterns, consider storing the regex in a variable for readability:
$pattern = 'error|warning|critical'
Get-Content .\logfile.txt | Where-Object { $_ -notmatch $pattern }
  1. Testing: Always test your regex patterns with sample data to ensure they work as expected before running them in a script.
  2. Escaping: Remember to escape special regex characters when you want to match them literally.

Conclusion

PowerShell’s Where-Object cmdlet with the -NotMatch operator is very much helpful for filtering objects that do not match specific patterns. In this PowerShell tutorial, I have explained everything about PowerShell where-object does not match, with examples.

You may also like: