PowerShell: Search Text in Files (grep)
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 -filter *.html | Select-String "joe|jane"
Search by Literal String
Match by literal string, use SimpleMatch
:
dir -recurse -filter *.html | Select-String "abc" -SimpleMatch
Case Sensitive Search
dir -recurse -filter *.html | Select-String "abc" -SimpleMatch -CaseSensitive
Multiple Search Patterns
dir -recurse -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 theSimpleMatch
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.