PowerShell - Filter results using Select-String

by
Jeremy Canfield |
Updated: February 02 2023
| PowerShell articles
Let's say C:\Users\john.doe\Documents\example.txt contains the follow two lines.
Hello
World
Get-Content can be used to read the content of a file. In this example, both lines are returned.
> Get-Content -path C:\Users\john.doe\Documents\example.txt
Hello
World
Select-String can be used to filter the results.
> Get-Content -path C:\Users\john.doe\Documents\example.txt | Select-String hello
Hello
Notice in the prior example that Select-String is case insensitive by default. The -CaseSensitive flag can be used to perform a case sensitive filter.
> Get-Content -path C:\Users\john.doe\Documents\example.txt | Select-String -CaseSensitive Hello
Hello
The -Pattern option can be used to perform a regular expression search, such as finding lines that being or end with a certain string.
> Get-Content -path C:\Users\john.doe\Documents\example.txt | Select-String -Pattern ^Hello
Hello
And here is how you would search for two (or more) strings.
> Get-Content -path C:\Users\john.doe\Documents\example.txt | Select-String 'Hello|World'
Hello
World
Did you find this article helpful?
If so, consider buying me a coffee over at