How to use PowerShell to (grep) Recursively Search for Text Within Files on Windows
Category : How-to
The 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.