Marshall’s Weblog

Embiggen Your Mind

Marshall’s Weblog header image 2

Emulating Grep in Powershell

December 6th, 2007 · 6 Comments

Grep is one of my favorite tools to help me find something in a set of files. Since I cannot download Cygwin at work, I have to make due with what I have.

The following is a translation of grep -R "mypattern" *.cpp for Powershell.

gci C:\path\to\files\* --include *.cpp -recurse | select-string -pattern "mypattern" -caseSensitive

<sarcasm>
So much easier to remember…
</sarcasm>

Tags: General

6 responses so far ↓

  • 1 Shiv // Jan 16, 2008 at 5:41 pm

    Perfect! Just what I was looking for, thanks!

  • 2 Ben // Apr 6, 2008 at 11:14 pm

    Great!
    Powershell syntax makes perfect sense when looked at from a “implementors” point of view, but from a practical standpoint…it’s rather ugly.

  • 3 Eddie // Apr 20, 2008 at 4:26 am

    Or you could use findstr

  • 4 Smiley // Apr 26, 2008 at 8:55 pm

    Findstr works well, as Eddie said.

    To make it more natural, I made a new alias called grep and make it execute findstr.

    “new-alias grep findstr”

    and now I can follow my instincts.

  • 5 Marshall // Apr 26, 2008 at 10:16 pm

    findstr works, but it is not Powershell specific. You can run findstr on cmd.exe on Windows 98 if you want. I was looking for a grep-like command that was 100% Powershell.

  • 6 IGuy // Jul 28, 2008 at 5:06 pm

    just do this:

    function grep($pattern,$filepath=”*.*”)
    {
    Get-ChildItem .\* -Include $filepath -Recurse | Select-String -Pattern $pattern -casesensitive
    }

    In your $profile…

    Not perfect but close enough for my needs at the moment.

Leave a Comment