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:

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:
- Case Sensitivity: By default,
-matchand-notmatchare case-insensitive in PowerShell. If you require case sensitivity, you can use thecprefix, as in-cnotmatch. - Performance: Filtering with
Where-Objectcan be slow on large collections. When possible, use cmdlet-specific filtering parameters which are usually more efficient. - 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 }- Testing: Always test your regex patterns with sample data to ensure they work as expected before running them in a script.
- 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:
- PowerShell Where-Object for Null or Empty Values
- PowerShell where-object in array
- PowerShell where-object not equal
- PowerShell Where-Object -NotLike Operator
- PowerShell where-object regex
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.