PowerShell Where-Object -NotLike Operator

One of the most useful cmdlets in PowerShell is Where-Object, which allows you to filter objects based on their properties. In this PowerShell tutorial, we will focus on the -NotLike operator used with Where-Object in PowerShell to exclude items that do not match a specified pattern. I will also show a few examples of the PowerShell Where-Object -NotLike operator.

The Where-Object -NotLike operator in PowerShell is used to filter out objects that do not match a specified wildcard pattern. For instance, to exclude files with a .txt extension from a list, you would use Get-ChildItem | Where-Object { $_.Name -NotLike ‘*.txt’ }. This cmdlet is extremely useful for refining search results and managing output in scripts effectively.

PowerShell Where-Object -NotLike

Where-Object in PowerShell filters the output of a command by evaluating a script block or expression against each object passed through the pipeline. If the expression returns $true, the object is passed along; if $false, the object is discarded.

The -NotLike Operator

The -NotLike operator is used with PowerShell Where-Object to filter out objects that do not match a pattern specified by a string with wildcard characters. The asterisk (*) is a common wildcard character that represents any number of characters.

For example, if you want to get a list of all files in a directory that do not have the .txt extension, you could use the following command:

Get-ChildItem -Path "C:\MyFolder\" -File | Where-Object { $_.Name -NotLike '*.txt' }

This command lists all items in the above directory but excludes files ending with .txt.

You can see the output in the screenshot below:

PowerShell Where-Object -NotLike

PowerShell Where-Object -NotLike Examples

Let’s explore some practical examples to see PowerShell Where-Object -NotLike in action.

Example 1: Filtering File Extensions

If you want to find all files in a directory that are not PowerShell scripts, you could use:

Get-ChildItem -Path C:\Scripts | Where-Object { $_.Extension -NotLike '.ps1' }

This command gets all items in the C:\Scripts directory and filters out files with the .ps1 extension.

Example 2: Excluding Specific Processes

To display all running processes except those named “chrome”:

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

This command gets all running processes and omits those with the process name “chrome”.

Example 3: Filtering Based on Multiple Conditions

You can combine -NotLike with other operators to create more complex filters. For instance, to find all services that are not stopped and do not start with “win”:

Get-Service | Where-Object { $_.Status -ne 'Stopped' -and $_.Name -NotLike 'win*' }

This command retrieves all services that are not in a “Stopped” state and whose names do not start with “win”.

Example 4: Excluding Items from a List

If you have a list of names and want to exclude certain names, you might use:

$names = 'John', 'Jane', 'Doe', 'Smith'
$names | Where-Object { $_ -NotLike 'Doe' }

This outputs all names except for “Doe”.

Once I executed the PowerShell script using VS code, you can see the output in the screenshot below:

PowerShell Where-Object -NotLike Examples

Example 5: Filtering Event Logs

To check the System event logs for entries that are not Information level:

Get-EventLog -LogName System | Where-Object { $_.EntryType -NotLike 'Information' }

This command gets all entries in the System event log and filters out those that are of the “Information” entry type.

Tips for Using Where-Object -NotLike

  1. Use Wildcards: The -NotLike operator supports wildcards, which makes it versatile for pattern matching. Use * to represent any number of characters.
  2. Case-Insensitive: By default, the -NotLike operator is case-insensitive. Use -CNotLike for case-sensitive comparisons.
  3. Combine with Other Operators: You can use -NotLike alongside other comparison operators (like -eq, -ne, -gt, -lt, etc.) to create complex filters.
  4. Optimize Performance: When working with large datasets, consider using other cmdlets like Select-String for text-based files or .Where() method for collections to optimize performance.

Conclusion

The Where-Object -NotLike operator is very useful in PowerShell for excluding items that don’t match a certain pattern. In this PowerShell tutorial, I have explained how to use the Where-Object -NotLike operator in PowerShell with a few examples.

You may also like: