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 -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 -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 -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.


Practical How-To

list files

list dirs

on dir

on file

Windows config


PowerShell in Depth

Programing PowerShell

Value Types





Data Structure

Loop and Iteration



Profile and Script

Script Examples