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-stringhas 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-patternis 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
SimpleMatchparameter. To absolutely get all matches, don't use theSimpleMatchparameter. Escape your-patternvalue 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
- PowerShell: 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 files
- PowerShell: list files by wildcard name pattern
- PowerShell: filter file name by regular expression
- PowerShell: sort files by size
- PowerShell: list large files 📜
- PowerShell: sort files by date
- PowerShell: search text in files (grep)