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"
[see List File by File Name Pattern]
Search by Literal String
Match by literal string, use
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
Match as literal string on value of
-pattern. (by default, the
-patternis interpreted as regex)
- Case sensitive. (by default, it's case-insensitive)
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
SimpleMatchparameter. To absolutely get all matches, don't use the
SimpleMatchparameter. Escape your
-patternvalue instead. See about_Regular_Expressions.
- List lines that do not match.
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.