Skip to content
Latchkey

Select-String: grep for PowerShell CI Logs

Select-String searches files or piped text for a regex pattern and returns the matching lines, the PowerShell equivalent of grep, with -Quiet for a boolean and -Context for surrounding lines.

To assert that a log contains (or does not contain) a string, or to scan source for a forbidden pattern, Select-String is the built-in grep on every Windows runner.

What it does

Select-String matches a regex -Pattern against the lines of input files or piped strings, returning MatchInfo objects (file, line number, line). -SimpleMatch treats the pattern as literal, -Quiet returns just true/false, and -Context shows lines around each hit.

Common usage

PowerShell
# search log files for errors
Select-String -Path .\logs\*.log -Pattern 'ERROR|FATAL'

# fail the build if a TODO/secret leaks into source
if (Get-ChildItem -Recurse -Filter *.cs |
      Select-String -Pattern 'AKIA[0-9A-Z]{16}' -Quiet) {
  throw "Possible AWS key committed"
}

# literal match with surrounding context
Select-String -Path build.log -SimpleMatch 'Build FAILED' -Context 2,2

Options

ParameterWhat it does
-Pattern <regex>Regex (or multiple) to match
-Path <files>Files to search (accepts wildcards)
-SimpleMatchTreat the pattern literally, not as regex
-QuietReturn $true/$false instead of match objects
-Context <pre,post>Show N lines before/after each match
-CaseSensitive / -NotMatchCase-sensitive search / invert the match

In CI

Use Select-String -Quiet inside an if to gate a step on whether a pattern appears, since it returns a clean boolean. Remember the pattern is regex by default, so escape special characters or add -SimpleMatch for literal strings like version numbers with dots.

Common errors in CI

A search returns nothing because the dot, parentheses, or brackets in your "literal" pattern were interpreted as regex; add -SimpleMatch. "parsing ... - Invalid pattern" means a malformed regex (an unescaped [ or (). Unlike grep, Select-String's exit code does not reflect match success, so test the returned objects or use -Quiet rather than $LASTEXITCODE to branch.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →