Tag Archives: unix

Emulating grep with PowerShell

I’m a big fan of PowerShell with it’s nifty object pipeline, but there are some quick and dirty tasks it could make easier. As and example, I was looking for a way to recursively search through directories, looking at the contents of files for a specific string.

Accomplishing this took a little poking around the Internet, but ultimately I found a great blog post describing how to do it. You need to to use a combination of get-childitem (abbreviated gci) and select-string. From Marshall’s post:

The grep command grep -R "mypattern" *.cpp can be translated to the PowerShell command:

gci . --include *.cpp -recurse | select-string -pattern "mypattern" -caseSensitive

Note that “.” can be replaced with an arbitrary path as opposed to the current directory and you might want to leave off -caseSensitive if you don’t care about case. You can also omit --include *.cpp to include all files.