Select-String
Use Select-String to search files, command output, and strings for a pattern — PowerShell's grep — and get the file, line number, and matching text back.
Documentation: Microsoft Learn — select-string
Syntax
Try it
PowerShell example · browser support is limited.
Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.
Examples
Find errors in log files
Show reference output
Illustrative output · values vary by environment
C:\Logs\app.log:142:2026-10-24 09:12:33 ERROR: database connection failed C:\Logs\app.log:301:2026-10-24 11:47:02 ERROR: timeout after 30 seconds
Search pipeline output for a literal string
Show reference output
Illustrative output · values vary by environment
Running Spooler Print Spooler
Show two lines of context around each match
Show reference output
Illustrative output · values vary by environment
Line 18: # Worker settings Line 19: # Updated 2026-10-01 > Line 20: MaxThreads=8 Line 21: Timeout=30 Line 22: Retries=3
Parameters
Parameter definitions:
-Pattern— The text or regular expression to search for. Accepts an array of patterns.-Path— Files to search. Accepts wildcards such as *.log.-SimpleMatch— Treats the pattern as literal text, not a regular expression. Use this when the pattern contains dots or brackets.-AllMatches— Returns every match on a line instead of only the first.-CaseSensitive— Makes the match case-sensitive. Matching is case-insensitive by default.-Context— Lines shown before and after each match, given as 'before, after'.-Quiet— Returns True if any match is found, False otherwise. Ideal for if statements.-List— Returns only the first match per file.
Regex is the default, and that surprises people
By default -Pattern is a regular expression, so Select-String -Pattern 'file.txt' treats the dot as 'any character' and matches 'fileatxt' too. When you mean literal text, always add -SimpleMatch, or escape the pattern with [regex]::Escape().
Each result is a MatchInfo object, not a plain string. Read .Filename, .LineNumber, .Line, and .Matches to build reports, and pipe to Select-Object when you only need certain fields.
Searching huge directories recursively is slow. Narrow with -Include and -Exclude, or pipe a filtered Get-ChildItem -Recurse list into Select-String so only the files you care about are read.
List files to search with Get-ChildItem
Read file contents with Get-Content
Updated 25 October 2026. Browser outputs are fictional; check the installed module help for your environment.
Recap
Select-String Use Select-String to search files, command output, and strings for a pattern — PowerShell's grep — and get the file, line number, and matching text back.
Next: Start Sleep