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"
$filteredNamesThis script will output:
John Smith
Emily SmithIn 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.

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 }
$filteredNamesHere, 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 BrownYou can also see the below screenshot, after I executed the PowerShell script.

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$" }
$filteredNamesIf you execute the above script, it will give you the output like below:
John Smith
Jason MartinIn 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*" }
$filteredNamesThis script will output:
Jane DoeThe -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.

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
$containsNameThis script will output:
TrueThe -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.
- Using Select-String
- Using Where-Object
- Using Regular Expressions
- Using -like and -notlike
- Using -contains
I hope this helps.
You may also like the following tutorials:
- Split a String and Get the First and Last Element in PowerShell
- Split String by Tab Character in PowerShell
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.