How to use PowerShell to (grep) Recursively Search for Text Within Files on Windows

How to use PowerShell to (grep) Recursively Search for Text Within Files on Windows

Category : How-to

Get Social!

powershellThe thing I find most annoying with Windows is that it isn’t Linux. Let’s forget the argument of free software, the interchangeable GUIs, the security and everything else which constitutes the usual Linux vs. Windows argument and focus on things I use everyday in Linux which are missing in Windows. Two major things come to mind; tail for monitoring logs and grep which is the easiest way to find something in a file.

Not having grep, more specifically grep -r, is challenging at best and almost reason enough to avoid the platform entirely.

With the introduction of PowerShell, Windows has given us the grep functionality albeit with a much less finesse than the Linux equivalent. You have to pipe multiple commands together; one command to transverse the directories, and one command to look for the pattern within each file found.

Use the below command inside the directory you would like to perform the ‘grep’ and change [SEARCH_PATTERN] to match what you would like to match.

dir -Recurse | Select-String -pattern [SEARCH_PATTERN]

For example:

dir -Recurse | Select-String -pattern "Find Me"

As you can see, its nowhere near the memorable Linux command grep -r but at least its now possible go get similar behaviour in a Windows environment.

Other Windows grep Binaries

There are also various Windows binaries which can be used from a standard command prompt however I had limited luck with each one. The biggest issue was that they require dependencies such as .NET which are not usually installed in server environments.


6 Comments

Mark Kroehler

17-Mar-2016 at 9:48 pm

Thanks for posting this. One minor critique: it should be -pattern, not – pattern.

    james.coyle

    17-Mar-2016 at 9:57 pm

    Thanks for the correction, Mark. Post updated.

dragon788

13-Jul-2016 at 9:27 pm

I was really frustrated by the fact that nobody has offered a way to “permanently” fix this issue, and all I wanted was a quick way to search for text matching my query. I ended up creating a simple function that does a vanilla grep.

function psgrep {gci *\*.* | sls -pattern $args[0]}
set-alias grep psgrep

Dan Hampleman

18-Mar-2020 at 2:14 pm

You need to remove the link to wingrep.com – the site has been hijacked by some other carpet-baggers.

    James Coyle

    23-Mar-2020 at 12:13 pm

    Thanks.

Andrew Bright

1-Feb-2023 at 11:11 pm

$Path = “enter the path you want to search”
$Text = “enter the text you want to search for in those files”
$PathArray = @()
$Results = “C:\temp\filenames.txt”

# This code snippet gets all the files in $Path that end in “.dat”.
Get-ChildItem $Path -Filter “*.dat” -Recurse |
Where-Object { $_.Attributes -ne “Directory”} |
ForEach-Object {
If (Get-Content $_.FullName | Select-String -Pattern $Text) {
$PathArray += $_.FullName
$PathArray += $_.FullName
}
}
$PathArray | % {$_} | Out-File $Results

This script will search for files in the path you specify – it will be a recursive search through subdirectories of that path.

Leave a Reply

Visit our advertisers

Quick Poll

Do you use ZFS on Linux?

Visit our advertisers