iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Use PowerShell’s Select-String cmdlet to find text in files. Search matching files in the current folder with a wildcard, or pipe files from Get-ChildItem -Recurse to search nested folders. Patterns are regular expressions by default; add -SimpleMatch when you want a literal search.
Search files in the current folder
Use -Path with a file pattern and -Pattern with the text or expression to find:
Select-String -Path .*.txt -Pattern 'Get-'
This searches matching .txt files in the current directory. For log files, for example, change the wildcard:
Select-String -Path .*.log -Pattern 'error'
-Path accepts wildcards. Passing a directory alone to -Path does not search all files inside it; specify a file pattern or enumerate the files first.
#1 Best Overall
- Book - powershell for sysadmins: workflow automation made easy
- Language: english
- Binding: paperback
Search nested folders
Use Get-ChildItem with -Recurse to supply files from the current directory and its subdirectories:
Get-ChildItem -Path . -Filter *.txt -Recurse |
Select-String -Pattern 'Microsoft'
Get-ChildItem enumerates the matching files, and Select-String searches their contents. Depending on your permissions, the command may report access-denied errors for folders or files it cannot read.
Choose literal text or a regular expression
By default, Select-String treats -Pattern as a regular expression. Characters that have special meaning in regex can therefore behave differently from ordinary text. Add -SimpleMatch for a literal search:
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Select-String -Path .*.txt -Pattern 'A.B' -SimpleMatch
Without -SimpleMatch, the period in A.B is a regex metacharacter rather than necessarily a literal period. For exact capitalization as well as literal matching, add -CaseSensitive:
Rank #3
Select-String -Path .*.txt -Pattern 'A.B' -SimpleMatch -CaseSensitive
Matches are case-insensitive by default. If capitalization matters, use -CaseSensitive. The -AllMatches option can find multiple occurrences on a line, but it is ignored when -SimpleMatch is used.
Read and control the results
When searching files, the default output is a MatchInfo object that includes the file name and line number. The search is line-based and reports the first match on each line by default.
Rank #4
Include surrounding lines
Use -Context to show lines before and after each match. The first number is the preceding-line count; the second is the following-line count:
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitchesSelect-String -Path .app.log -Pattern 'timeout' -Context 2,3
Return only whether a match exists
Use -Quiet when you need a yes-or-no result instead of MatchInfo details:
Best Value
Select-String -Path .*.txt -Pattern 'needle' -Quiet
It returns $true when a match is found and $null when no match is found.
Return one matching instance per file
Use -List to return only the first matching instance per input file. This is useful when you need to identify which files contain a match rather than collect every matching line.
Handle paths and file encoding
Quote paths that contain spaces. If a path includes wildcard characters that should be treated literally, use -LiteralPath instead of -Path; -LiteralPath does not interpret wildcard characters.
Microsoft’s overview says Select-String detects a byte-order mark (BOM) and assumes BOM-less files are 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 with -Encoding rather than assuming all PowerShell versions handle every file identically.
When searching piped objects
Piping file objects from Get-ChildItem to Select-String searches file contents. For other objects, their string representation may differ from the formatted text shown in the console. If you specifically need to search formatted output, Microsoft documents piping through Out-String or Out-String -Stream before Select-String.
Quick Recap
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.

