PowerShell: Search Text in Files (grep)

By Xah Lee. Date: . Last updated: .

Here's ways to search a string in file content of files in a dir. (like linux grep)

Search by regex

List all occurences of Regular Expression pattern of the content of html files in a directory:

dir -recurse -file -filter *.html | Select-String "joe|jane"

[see List File by File Name Pattern]

Search by Literal String

Match by literal string, use SimpleMatch:

dir -recurse -file -filter *.html | Select-String "abc" -SimpleMatch

Case Sensitive Search

dir -recurse -file -filter *.html | Select-String "abc" -SimpleMatch -CaseSensitive

Multiple Search Patterns

dir -recurse -file -filter *.html | Select-String regex1, regex2

Common Select-String parameters

-SimpleMatch

Match as literal string on value of -pattern. (by default, the -pattern is interpreted as regex)

-CaseSensitive

Case sensitive. (by default, it's case-insensitive)

-AllMatches

Find all matches in a line. (by default, only first match in a line is found.) This parameter is ignored when used in combination with the SimpleMatch parameter. To absolutely get all matches, don't use the SimpleMatch parameter. Escape your -pattern value instead. See about_Regular_Expressions.

-NotMatch

List lines that do not match.

Note: Select-String tries to match string by line units in input text. You cannot use it to search block of text that contains newline character.

PowerShell, List Files