PowerShell: How to Use -notlike With Multiple Values


You can use the -notlike operator in PowerShell to filter for strings that do not match a particular pattern.

You can use the -and operator along with the -notlike operator to filter for strings that do not match multiple patterns:

Import-Csv $my_file | ? {$_.team -notlike "*ers*" -and $_.team -notlike "*ets*"}

This particular example imports the CSV file saved at the location specified by the $my_file variable, then filters the rows of the file to only display the ones where the string in the team column does not match either of the following patterns:

  • “ers”
  • “ets”

The following example shows how to use this syntax in practice.

Example: How to Use -notlike With Multiple Values in PowerShell

Suppose we have a CSV file saved at the following location:

  • c:\users\bobbi\nba_data.csv

Suppose we use the Import-Csv cmdlet to view the contents of this entire file:

The file contains three columns that show the team, points and assists for various basketball players.

Suppose that we would like to only import the rows where the string in the team column does not contain either of the following patterns:

  • “ers”
  • “ets”

We can use the following syntax to do so:

$my_file = "c:\users\bobbi\nba_data.csv"
Import-Csv $my_file | ? {$_.team -notlike "*ers*" -and $_.team -notlike "*ets*"}

The following screenshot shows how to use this syntax in practice:

PowerShell not like multiple values

Notice that any rows where the string in the team column contained “ers” or “ets” have been excluded from the output.

Specifically, the following team names were filtered out:

  • Lakers
  • Nets
  • Rockets

Note: In this example we used the -and operator to filter out strings that matched two particular patterns, but you can use the -and operator as many times as you would like to filter out even more patterns.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: A Simple Formula for “If Like”
PowerShell: A Simple Formula for “Not Contains”
PowerShell: How to Replace Text in String
PowerShell: How to Replace Special Characters in String

Leave a Reply