Search log files in PowerShell with Select-String and SimpleMatch
- Published: 2026.10.16
- Updated: 2026.10.16
- Windows
Searching a log in an editor gets awkward when you need both the matching message and its line number. PowerShell’s Select-String can return those details directly from a file. The search text is a regular expression by default, so a dot in an error message can also match an unexpected character.
Start with a literal search, then add pattern matching or case sensitivity when the task calls for it. The examples below use syntax available in Windows PowerShell 5.1 and PowerShell 7.
Search for literal text
Save the following five lines as sample[1].log, using UTF-8, in your working folder.
INFO start
ERROR a.b
error axb
WARN finish
ERROR repeat repeat
$hits = Select-String -LiteralPath '.\sample[1].log' -Pattern 'a.b' -SimpleMatch -Encoding UTF8
$hits | Select-Object LineNumber, Line
This returns line 2, ERROR a.b. LineNumber identifies the line in the file, while Line contains the whole matching line. -SimpleMatch treats the search text literally. -LiteralPath applies to the file name instead, letting the brackets in this example remain ordinary characters.
Use regular expressions deliberately
Select-String -LiteralPath '.\sample[1].log' -Pattern 'a.b' -Encoding UTF8 |
Select-Object LineNumber, Line
Without -SimpleMatch, a.b matches both a.b on line 2 and axb on line 3: the dot represents any one character. For an ordinary error message or punctuation, a literal search is often easier to maintain.
Select-String -LiteralPath '.\sample[1].log' -Pattern '^ERROR ' -Encoding UTF8 |
Select-Object LineNumber, Line
The caret anchors this pattern to the start of a line. Lines 2, 3 and 5 match because searches ignore case by default.
Match uppercase and lowercase exactly
Select-String -LiteralPath '.\sample[1].log' -Pattern 'ERROR' -SimpleMatch -CaseSensitive -Encoding UTF8 |
Select-Object LineNumber, Line
Adding -CaseSensitive leaves lines 2 and 5. The lowercase error on line 3 no longer matches. Use this when capitalization is part of the condition, rather than enabling it automatically for every log search.
Check whether a match exists
Select-String -LiteralPath '.\sample[1].log' -Pattern 'ERROR' -SimpleMatch -Quiet -Encoding UTF8
Select-String -LiteralPath '.\sample[1].log' -Pattern 'FATAL' -SimpleMatch -Quiet -Encoding UTF8
True
False
The sample contains ERROR but no FATAL, producing the values above. Without -Quiet, a search with no matching line normally emits no result objects. A missing or unreadable file produces an error; that is a separate condition from a successful search that found nothing.
Find multiple matches on one line
$hits = Select-String -LiteralPath '.\sample[1].log' -Pattern 'repeat' -AllMatches -Encoding UTF8
$hits | ForEach-Object { $_.Matches.Count }
Line 5 contains repeat twice, so the count is 2. -AllMatches populates the Matches collection; it does not emit a duplicate result row for every occurrence. Combining it with -SimpleMatch ignores -AllMatches, so this example uses regular-expression matching.
Check unexpected results
For extra matches, inspect punctuation in the pattern. For missing lines, check case sensitivity and the spelling of the search text. If characters look wrong, match -Encoding to the file’s actual encoding; the sample here is UTF-8. For file errors, check the working folder and the path first. Passing the file path directly also keeps the original file line numbers available.
References
-
Previous article
Back up WSL and restore under a new name without removing Ubuntu 2026.10.15
-
Next article
Join paths in PowerShell without confusing joining and existence checks 2026.10.16