The PowerShell grep equivalent is Select-String, also available as the alias sls. Run Select-String -Path .\app.log -Pattern 'error' to print every matching line with its file name and line number.
Microsoft's own documentation says to use Select-String "similar to grep in Unix or findstr.exe in Windows". It works on lines of text, but it differs from grep in several ways that catch people out: it ignores case by default, it returns objects rather than plain text, and it will not search a folder unless you tell it which files to read. This guide covers each difference, with the flag-by-flag mapping from grep.
The basic command
Select-String -Path .\app.log -Pattern 'error'
Example output:
app.log:3:ERROR connection refused: db01
app.log:6:Error: timeout after 30s
app.log:9:no errors found in cache check
Each result is shown as file:line number:text. Both -Path and -Pattern are positional, so the shortest form is sls 'error' .\app.log, with the pattern first. Notice that all three spellings of "error" matched, because the search ignores case.
The -Path parameter accepts wildcards, so .\*.log searches every log file in the current folder. If you pass only a directory, the command fails: the documentation says you must specify files in the directory, such as *.doc or *.*.
Select-String flags compared with grep
| You want (grep) | Select-String |
|---|---|
| Case-sensitive match (grep's default) | -CaseSensitive |
grep -v, lines that do not match | -NotMatch |
grep -F, literal text | -SimpleMatch |
grep -C 2, lines around a match | -Context 2 |
grep -B 2 -A 3 | -Context 2,3 (before, then after) |
grep -l, only file names | -List (first match per file) |
grep -q, yes or no | -Quiet (returns $true or $null) |
grep -r, search folders | Get-ChildItem -Recurse | Select-String |
grep -o, only the matched part | -AllMatches and read .Matches.Value |
| Output plain lines only | -Raw (PowerShell 7 and later) |
grep -e a -e b, several patterns | -Pattern 'a','b' |
There is no -n equivalent because line numbers are always included, and no -c count switch. Counting is covered below.
How to make Select-String case-sensitive
Select-String does not distinguish upper and lower case unless you ask it to:
'Hello', 'HELLO' | Select-String -Pattern 'HELLO' -CaseSensitive
This is the documentation's own example, and it returns only HELLO. The same default holds across PowerShell's regular expressions: the about_Regular_Expressions page says they are case-insensitive by default, with -CaseSensitive for Select-String and -cmatch for the match operator.
Search a folder and its subfolders (grep -r)
Select-String reads files, but it does not walk folders itself. Let Get-ChildItem list the files and pipe them in. File objects are treated as paths, so Select-String reads each file's contents instead of its name:
Get-ChildItem -Path .\src -Include *.py -Recurse -File | Select-String -Pattern 'TODO'
Example output:
src\app\main.py:14: # TODO: handle empty input
src\app\utils.py:52: # TODO remove after migration
Microsoft's own recursive example notes that, depending on your permissions, you might see "Access denied" messages in the output when searching system folders.
List only the files that contain a match (grep -l)
Get-ChildItem -Recurse -File -Filter *.config | Select-String -Pattern 'password' -List | Select-Object -ExpandProperty Filename
-List stops at the first match in each file. The documentation calls it the most efficient way to get a list of files whose contents match. Every result is a MatchInfo object, and Filename and Line are among its properties. To see the full list, run Select-String -Path test.txt -Pattern 'test' | Get-Member.
Show lines before and after a match (grep -A, -B, -C)
Select-String -Path .\app.log -Pattern 'timeout' -Context 2,3
The first number is the count of lines before the match and the second is the count after. With one number, that many lines are shown on both sides. In the default display, matching lines are marked with a > in the first column, and the unmarked lines are context. The context does not change how many objects come out. Each match is still one MatchInfo object, and the surrounding lines are stored in its Context property.
One consequence: if you pipe the result into a second Select-String, the second command searches only the matched line, not the context lines, so -Context has no use there.
Exclude lines (grep -v) and combine patterns
Select-String -Path .\app.log -Pattern 'DEBUG' -NotMatch
Pass several patterns as an array and you get matches for each one, which is an OR:
Select-String -Path .\app.log -Pattern 'ERROR','WARN'
For AND, chain two commands. The second searches the matched line of the first, as described above:
Select-String -Path .\app.log -Pattern 'ERROR' | Select-String -Pattern 'db01'
Search for literal text or special characters
The pattern is a regular expression, so a dot matches any character and a question mark is a quantifier. Searching for a literal ? without escaping it matches nothing useful. You have two options:
Select-String -Path .\notes.txt -Pattern 'v1.2.3' -SimpleMatch
Select-String -Path .\notes.txt -Pattern 'what\?'
The first treats the text as plain characters. The second escapes the question mark with a backslash, as the documentation does in its own example. Use single quotes around patterns that contain $, for example '^fish$'. Inside double quotes PowerShell tries to expand $ as a variable before Select-String sees it. Note that -AllMatches is ignored when you also use -SimpleMatch.
Search the output of another command
This is where the difference from grep is easiest to hit. In a Unix shell, ps | grep node works on text. In PowerShell the pipeline carries objects, and Select-String converts each one with ToString(), which is not the table you see on screen. Microsoft's example shows a hashtable piped straight into Select-String producing no output at all.
The fix is Out-String -Stream, which turns the formatted output into one string per line:
Get-Process | Out-String -Stream | Select-String -Pattern 'chrome'
Without -Stream, Out-String produces a single multi-line string, so a match prints the whole block. Often there is a better option: filter on a real property and skip text search entirely.
Get-Process | Where-Object { $_.ProcessName -like '*chrome*' }
Use Select-String when you are searching text, such as files, logs or command output from a native program like ipconfig /all. Use Where-Object when the thing you are filtering already has properties.
Count matches and use the result in a script
Select-String returns one object per matching line, so wrapping the command in parentheses and reading .Count gives the number of matching lines, the equivalent of grep -c:
(Select-String -Path .\app.log -Pattern 'ERROR' -CaseSensitive).Count
Wrapping the call in @( ) forces the result into an array, which makes the count reliable when there are zero or one matches:
@(Select-String -Path .\app.log -Pattern 'ERROR' -CaseSensitive).Count
For a simple yes or no in an if statement, use -Quiet:
if (Select-String -Path .\app.log -Pattern 'FATAL' -Quiet) { 'Found a fatal error' }
By default only the first match on each line is recorded. Add -AllMatches to record every match on a line. The command still returns one object per line, but its Matches property then holds all the matches, so .Matches.Value gives the matched text, similar to grep -o.
Get plain lines instead of objects (PowerShell 7)
Select-String -Path .\app.log -Pattern 'ERROR' -Raw
The -Raw switch outputs only the matching strings instead of MatchInfo objects. The documentation describes this as the behavior most similar to Unix grep or findstr.exe. It was introduced in PowerShell 7, so it does not exist in Windows PowerShell 5.1, which ships with Windows. If you get an error about a missing parameter, check your version with $PSVersionTable.PSVersion.
Encoding problems
Select-String detects the encoding from the file's byte-order mark (BOM). If there is no BOM, it assumes UTF-8. A file saved as UTF-16 without a BOM, or in a legacy code page, can therefore appear to contain no matches. Tell it what to expect with -Encoding, for example -Encoding unicode for UTF-16 little-endian, or a code page such as -Encoding windows-1251 in PowerShell 6.2 and later.
Other grep options on Windows
If you need the full GNU grep behavior, run real grep through WSL or the copy bundled with Git for Windows. For a side-by-side look at grep itself, including a findstr column, see grep command examples. If you are new to the shell, PowerShell basic commands covers the pipeline and objects that Select-String builds on.
If it still fails
- No output and no error. Check the pattern for unescaped regex characters, and try
-SimpleMatch. Then check encoding. - Too many matches. Remember it ignores case. Add
-CaseSensitive. - Path error on a folder. Add a wildcard such as
.\logs\*, or useGet-ChildItemand pipe. - Parameter not found.
-Raw,-NoEmphasisand-Cultureneed PowerShell 7.
Frequently asked questions
- Is Select-String case-sensitive?
- No. Select-String ignores case by default, which is the opposite of grep. Add the -CaseSensitive switch to make the match exact. The same default applies to every regular expression in PowerShell.
- Does Select-String use regular expressions or plain text?
- Regular expressions, using the .NET regex engine. Add -SimpleMatch to treat the pattern as literal text, which is the closest match to grep -F. Characters such as the dot, question mark and brackets have special meaning unless you use -SimpleMatch or escape them with a backslash.
- Why does Select-String say nothing when I pipe an object into it?
- Piped objects are converted with ToString(), which is not the table you see on screen. For command output, pipe through Out-String -Stream first so each displayed line becomes its own string.
- Can I use Select-String on Linux or macOS?
- Yes. Select-String is part of PowerShell 7, which runs on Windows, macOS and Linux, and it behaves the same way there. The -Raw and -NoEmphasis switches exist only in PowerShell 7 and later, not in Windows PowerShell 5.1.