Home » Using PowerShell Where-Object with -NotLike

Using PowerShell Where-Object with -NotLike

You can use the Where-Object cmdlet along with the -NotLike operator to filter elements based on a pattern that should not match.

The -notlike operator is used for pattern matching with wildcards to find elements that do not match a specified pattern.

The following method shows how you can do it with syntax.

Method 1: Use Where-Object to filter elements not like

$languages = @("Python", "C#", "C", "C++", "VB", "Java", "Go")

$result = $languages | Where-Object { $_ -notlike "C*" }

$result

This example will filter out elements that do not contain the string “C*” using the -notlike operator with the pattern “C*“.

The following example shows how you can use this method.

Use Where-Object to Filter Elements not like

The following PowerShell script shows how you can do it.

# define list
$languages = @("Python", "C#", "C", "C++", "VB", "Java", "Go")

# Use Where-Object to filter elements not like "C*"
$result = $languages | Where-Object { $_ -notlike "C*" }

# Output the result
$result

Output:

PowerShell Where-Object with not-like operator
PowerShell Where-Object with not-like operator

In this PowerShell example, $languages is an array of strings representing different programming languages.

The Where-Object cmdlet filters out elements that do not contain the string “C*” using the notlike operator with the pattern “C*“.

The result is stored in the $result variable.

The output displays all other elements other than C, C++ and C#.

Conclusion

I hope the above article on using the PowerShell Where-Object cmdlet with the -notlike operator to filter out elements is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.