Command library / Select-String
Select-String
Search text in files for a pattern
PowerShell's grep. It searches files (or piped text) for a pattern and returns each match with its file name and line number. Matching ignores case by default, and the pattern is a regular expression unless you add -SimpleMatch.
Practice Select-StringUsage
Options and flags
-PatternPATTERNwhat to look for, as a regular expression (first positional parameter)
-PathFILEthe file(s) to search; wildcards allowed (second positional parameter)
-NotMatchreturn the lines that do NOT match
-SimpleMatchtreat the pattern as plain text, not a regular expression
-ContextNalso show N lines before and after each match
Examples
Select-String -Path app.log -Pattern errorEvery line of app.log that mentions error, in any case.
Select-String error app.logThe same search with positional parameters: the pattern first, then the file.
Select-String -Path security.log -Pattern "Access denied" -Context 2Each match with two lines of context on either side.
Common mistakes
Positionally the pattern comes first and the file second; swapped, it searches a file named error for the text app.log.