Files

Search File Contents Recursively with PowerShell (grep)

3 min read

Select-String is PowerShell’s grep — search inside files for text or regex.

Search a folder recursively

Get-ChildItem C:\app -Recurse -Filter *.config |
  Select-String -Pattern "connectionString"

Simple one-path search (with wildcards)

Select-String -Path "C:\logs\*.log" -Pattern "ERROR"

Case-insensitive is the default; for case-SENSITIVE

Select-String -Path .\*.txt -Pattern "TODO" -CaseSensitive

Regex match

Select-String -Path .\*.log -Pattern "\b\d{1,3}(\.\d{1,3}){3}\b"   # IPv4 addresses

Just the filenames that contain a match

Get-ChildItem -Recurse -File | Select-String -Pattern "api_key" -List |
  Select-Object -ExpandProperty Path

Show surrounding context (2 lines each side)

Select-String -Path .\app.log -Pattern "Exception" -Context 2,2

Notes: -Pattern is regex by default — use -SimpleMatch to search a literal string with special characters. -List stops at the first match per file (fast when you only need “which files”). For very large/binary trees, add -Filter/-Include on Get-ChildItem to limit what’s scanned.

Open the full version (with copy buttons) ↗

← All recipes