Today I will show you how to search for specified patterns in text files. We can say that it will be similar to known findstr in traditional cmd.
In PowerShell there is a command: Select-String
As a example file I will use text file with lorem ipsum content.
Example command which will find pattern will be like that:
1 |
select-string -Path .\sample.txt -Pattern "est" |
We also can search in all files in specified location.
1 |
select-string -Path .\*.txt -Pattern "est" |
Take a look, that by default we see file name, line number and content but we can display additional params.
1 |
select-string -Path .\*.txt -Pattern "est" | get-member |
I choose most usable:
1 |
select-string -Path .\*.txt -Pattern "est" | select linenumber, line, path, filename C:\Temp\text2.txt text2.txt |
Also we can use param called -Exclude, where we can specify files we dont want to search.
1 |
select-string -Path .\*.txt -Pattern "est" -Exclude "*sample*"| select linenumber, line, path, filename |