Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

PowerShell Select-String: Find Text in Files Fast

Find text in files with PowerShell Select-String, including wildcard and recursive searches, literal matching, case controls, context lines, and output options.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use PowerShell’s Select-String cmdlet to find text in files. Search files in the current folder with a wildcard path, or pipe files from Get-ChildItem -Recurse to search nested folders. Patterns are regular expressions by default; add -SimpleMatch when you want literal text.

Search files in the current folder

Give Select-String a file path or wildcard and the text pattern to find. This searches every matching text file in the current directory and reports matching lines with file and line information.

As an Amazon Associate I earn from qualifying purchases.

Select-String -Path .*.txt -Pattern 'Get-'

For log files, change the wildcard and pattern:

Select-String -Path .*.log -Pattern 'error'

-Path accepts wildcards, but a directory by itself is not a file search path. Use a file pattern such as .*.txt or enumerate files with Get-ChildItem. Quote paths that contain spaces.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Search nested folders recursively

Use Get-ChildItem to enumerate files under a folder, then pipe those file objects to Select-String. The following searches all .txt files below the current directory, including subdirectories:

#1 Best Overall
Sale
PowerShell for Sysadmins: Workflow Automation Made Easy
  • Book - powershell for sysadmins: workflow automation made easy
  • Language: english
  • Binding: paperback
Get-ChildItem -Path . -Filter *.txt -Recurse |
    Select-String -Pattern 'Microsoft'

Replace . with the folder you want to search. Depending on your permissions, PowerShell may also display access-denied messages for folders it cannot read.

Choose literal text or a regular expression

-Pattern treats its value as a regular expression by default. That is useful for pattern matching, but characters such as . have special meaning in regular expressions. For an ordinary phrase or exact sequence of characters, use -SimpleMatch.

Select-String -Path .*.txt -Pattern 'Get-' -SimpleMatch

Combine literal matching with case-sensitive matching when capitalization must also be exact:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Select-String -Path .*.txt -Pattern 'A.B' -SimpleMatch -CaseSensitive

Without -CaseSensitive, matching ignores capitalization. Without -SimpleMatch, A.B is interpreted as a regex, where the period can match a character rather than only a period.

Show context or change the output

By default, Select-String returns MatchInfo objects and reports the first match on each matching line. These options adjust what you get back:

  • Show nearby lines: -Context 2,3 displays two lines before and three after each match.
  • Return a match indication: -Quiet returns $true if a match is found and $null if none is found, instead of MatchInfo details.
  • Stop after a file’s first match: -List returns only the first matching instance per input file.
  • Find multiple matches on one line: -AllMatches reports every match on a line; it is ignored when -SimpleMatch is used.

For example, to inspect lines around a timeout in a log:

Select-String -Path .app.log -Pattern 'timeout' -Context 2,3

To check whether any text file contains a term without displaying matching lines:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Select-String -Path .*.txt -Pattern 'needle' -Quiet
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle paths and file encoding carefully

Use -LiteralPath instead of -Path when the path must be treated exactly as typed and wildcard characters in it should not be expanded. Use -Path for wildcard searches such as .
eports*.txt
.

Encoding behavior depends on the PowerShell version and file. Microsoft’s overview says BOM detection is used and BOM-less files are assumed to be UTF-8; the PowerShell 7.6 parameter reference lists utf8NoBOM as the default for -Encoding. If you know a file uses a different encoding, specify it explicitly with -Encoding rather than assuming every PowerShell release handles it identically.

Select-String is intended to search input strings and file contents. When piping other kinds of objects, their string representation may differ from what formatted output shows. If you need to search formatted output, Microsoft documents piping through Out-String or Out-String -Stream.

Quick choice guide

Goal Use
Search matching files in the current folder Select-String -Path .*.txt -Pattern 'text'
Search files in subfolders too Get-ChildItem -Path . -Filter *.txt -Recurse | Select-String -Pattern 'text'
Match a literal phrase or punctuation Add -SimpleMatch
Require exact capitalization Add -CaseSensitive
See surrounding lines Add -Context before,after
Check only whether a match exists Add -Quiet

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.