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 Text by Regex, a Single File
search by Regular Expression
Select-String -Path "~/alice.txt" -Pattern 'rabbit'
select-string
has aliassls
🛑 WARNING: Select-String
search based on lines.
by default, it won't find text that span multiple lines.
Search Text that Span Multiple Lines
# search text that span multiple lines Get-Content "~/blog.html" -raw | Select-String '(?s)<ul>.<li>' # (?s) is regex flag. it means, make the dot to also match newline char
Search by Regex, Multiple Files
dir -recurse -file -filter *.html | Select-String "joe|jane"
〔see also List File by File Name Pattern〕
Search by Literal String
parameter SimpleMatch
for match by literal string
dir -recurse -file -filter *.html | Select-String "alice" -SimpleMatch
Case Sensitive Search
dir -recurse -file -filter *.html | Select-String "Alice" -SimpleMatch -CaseSensitive
Search Multiple text 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 theSimpleMatch
parameter. Escape your-pattern
value instead. See about_Regular_Expressions. -NotMatch
-
List lines that do not match.
PowerShell. List Dirs and Files
List Dirs
- PowerShell: Navigate Directory
- PowerShell: Show Current Dir Path
- PowerShell: List Directories
- Show Directory as Tree
- PowerShell: List Empty Dir 📜
- PowerShell: Dir Size 📜
List Files
- PowerShell: List Files
- PowerShell: Show Fullpath, No Truncate Lines
- PowerShell: List Empty Files
- PowerShell: Count Number of Files
- PowerShell: List Files by Wildcard Name Pattern
- PowerShell: Filter File Name by Regular Expression
- PowerShell: List File by Size
- PowerShell: Sort Files by Size 📜
- PowerShell: List Files by Date Time
- PowerShell: Search Text in Files (grep)