PowerShell Genie

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.

Command 4 min read PowerShell 7 on Windows

Documentation: Microsoft Learn — select-string

Syntax

1PS> Select-String [-Pattern] <String[]> [-Path] <String[]> [-AllMatches] [-CaseSensitive] [-SimpleMatch] [-Context <Int32[]>] [-List] [-Quiet] [-Include <String[]>] [-Exclude <String[]>] [<CommonParameters>]
2PS> Select-String [-Pattern] <String[]> -InputObject <PSObject> [-AllMatches] [-CaseSensitive] [-SimpleMatch] [-Context <Int32[]>] [-Quiet] [<CommonParameters>]

Try it

Windows PowerShell

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.

Interactive simulation · fictional data

Examples

Find errors in log files

1PS> Select-String -Path C:\Logs\*.log -Pattern 'error'
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

1PS> Get-Service | Select-String -Pattern 'Spooler' -SimpleMatch
Show reference output

Illustrative output · values vary by environment

Running  Spooler  Print Spooler

Show two lines of context around each match

1PS> Select-String -Path .\config.txt -Pattern 'MaxThreads' -Context 2, 2
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.

Microsoft Learn — select-string

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
Genie guideSelect-String

Select-String searches text for a regex pattern and returns MatchInfo objects with the file name, line number, and line text. Learn literal vs regex search, context lines, and the -Quiet true-or-false check. The console below is pre-loaded — run it, then change a value and run it again.