Tag: grep

Emulating grep with PowerShell

by Warlock on May.07, 2009, under 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.

1 Comment :, more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...