How to Filter Strings in PowerShell?

You should know about filtering strings in PowerShell if you are working with strings. PowerShell provides different methods and cmdlets to filter strings in PowerShell. In this tutorial, I will show you how to filter strings in PowerShell with various examples.

To filter strings in PowerShell using Select-String, you can pipe the string to the cmdlet. For example, to find the word “error” in a given string, you can use the following command:

"An example string with an error in it" | Select-String -Pattern "error"

This command searches the string for the word “error” and displays the matching lines. This is useful for quickly filtering and identifying specific patterns within strings.

How to Filter Strings in PowerShell

Let us check each method with examples. You can try these scripts using any of the editors like VS code or PowerShell ISE.

I have tried here to take different types of example for each method.

1. Using Select-String

The Select-String cmdlet in PowerShell can be used to filter text or string. It’s similar to the grep command in Unix-based systems and allows you to search for text patterns using regular expressions.

Here is a complete example.

Suppose you have a list of names and want to filter out names containing the string “Smith”.

Write the script like below:

$names = @(
    "John Smith",
    "Jane Doe",
    "Michael Johnson",
    "Emily Smith",
    "William Brown"
)

$filteredNames = $names | Select-String -Pattern "Smith"
$filteredNames

This script will output:

John Smith
Emily Smith

In this example, Select-String searches each string in the $names array and returns those that match the pattern “Smith”.

I executed the above script using VS code; you can see the output in the screenshot below.

Filter Strings in PowerShell

Read Split a String by Word in PowerShell

2. Using Where-Object

Another method you can use to filter string in PowerShell, is by using the Where-Object cmdlet.

The Where-Object cmdlet is used for filtering objects, including strings. It allows you to use more complex conditions to filter your data.

Let us see an example of how to filter a string based on length.

Let’s say you want to filter names that are longer than 10 characters.

$names = @(
    "John Smith",
    "Jane Doe",
    "Michael Johnson",
    "Emily Smith",
    "William Brown"
)

$filteredNames = $names | Where-Object { $_.Length -gt 10 }
$filteredNames

Here, Where-Object searched for each string in the $names array and returns those with a length greater than 10 characters.

Michael Johnson
Emily Smith
William Brown

You can also see the below screenshot, after I executed the PowerShell script.

How to Filter Strings in PowerShell

3. Using Regular Expressions

PowerShell supports regular expressions (regex), which can be used for more advanced string filtering. You can use the -match operator for this purpose.

Let me show you an example of how to filter a string with Regex.

Suppose you want to filter names that start with “J” and end with “n”.

$names = @(
    "John Smith",
    "Jane Doe",
    "Michael Johnson",
    "Emily Smith",
    "William Brown",
    "Jason Martin"
)

$filteredNames = $names | Where-Object { $_ -match "^J.*n$" }
$filteredNames

If you execute the above script, it will give you the output like below:

John Smith
Jason Martin

In this example, the regex pattern ^J.*n$ matches strings that start with “J” and end with “n”.

Read Split a String into Variables in PowerShell

4. Using -like and -notlike

The -like and -notlike operators are simpler alternatives to regex for basic wildcard matching in PowerShell strings.

Here is an example of filtering a string with wildcards.

Let’s filter names that contain “Doe”.

$names = @(
    "John Smith",
    "Jane Doe",
    "Michael Johnson",
    "Emily Smith",
    "William Brown"
)

$filteredNames = $names | Where-Object { $_ -like "*Doe*" }
$filteredNames

This script will output:

Jane Doe

The -like operator uses the * wildcard to match any string that contains “Doe”.

You can also refer to the screenshot below after I executed the above PowerShell script.

PowerShell filter strings

5. Using -contains

PowerShell also provides the -contains operator to split a string.

The -contains operator checks if a collection contains a specific item. This is useful for exact matches.

Here is an example of an exact matches in a string.

$names = @(
    "John Smith",
    "Jane Doe",
    "Michael Johnson",
    "Emily Smith",
    "William Brown"
)

$searchName = "Jane Doe"
$containsName = $names -contains $searchName
$containsName

This script will output:

True

The -contains operator checks if the $names array contains the exact string “Jane Doe”.

Conclusion

In this PowerShell tutorial, I have explained how to split strings in PowerShell using the below methods.

  1. Using Select-String
  2. Using Where-Object
  3. Using Regular Expressions
  4. Using -like and -notlike
  5. Using -contains

I hope this helps.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.