PowerShell Select-String
File and stream text search with Select-String — the PowerShell grep equivalent.
Select-String Quick Reference (PowerShell grep)
Basic pattern search
Select-String -Path *.log -Pattern "ERROR"
Select-String -Path *.log -Pattern "ERROR" -CaseSensitive
Recursive search
Get-ChildItem -Recurse -Filter *.adoc | Select-String -Pattern "hardcoded"
Context lines (like grep -A/-B/-C)
Select-String -Path auth.log -Pattern "Failed" -Context 2,3
# 2 lines before, 3 lines after
Extract matched value only
Get-Content log.txt | Select-String -Pattern '\d+\.\d+\.\d+\.\d+' -AllMatches |
ForEach-Object { $_.Matches.Value } | Sort-Object -Unique
Multiple patterns
Select-String -Path *.log -Pattern "ERROR", "FATAL", "CRITICAL"
Invert match (like grep -v)
Select-String -Path config.txt -Pattern "^#" -NotMatch
# Lines NOT starting with #
Search with file filtering
Get-ChildItem -Recurse -Include *.ps1, *.psm1 |
Select-String -Pattern "Invoke-Command" |
Select-Object Path, LineNumber, Line
Pipe to object manipulation (the PowerShell advantage over grep)
Select-String -Path *.log -Pattern "Failed login from (?<ip>\d+\.\d+\.\d+\.\d+)" |
ForEach-Object { $_.Matches.Groups['ip'].Value } |
Group-Object | Sort-Object Count -Descending | Select-Object -First 10
# Top 10 source IPs for failed logins — structured, not text