PowerShell: Search String in Files

By Xah Lee. Date: . Last updated: .

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

Search all directory and subdirectory

List all occurences of regex pattern of html files in a directory:

Get-ChildItem -Recurse -Include *.html | Select-String -Pattern abc|xyz

Search Literal String

Match by literal string, use SimpleMatch:

Get-ChildItem -Recurse -Include *.html | Select-String -Pattern abc -SimpleMatch

Case Sensitive Search

Get-ChildItem -Recurse -Include *.html | Select-String -Pattern abc -SimpleMatch -CaseSensitive

Multiple Search Patterns

Get-ChildItem -Recurse -Include *.html | Select-String -Pattern regex1, regex2

Common Select-String parameters

Match as literal string on value of -Pattern. (by default, the -Pattern is 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 SimpleMatch parameter. To absolutely get all matches, don't use the SimpleMatch parameter. Escape your -pattern value instead. See about_Regular_Expressions.
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.




Script Examples