Saturday, April 5, 2008

Powershell Grep

For whatever reason I always forget the syntax to quickly find text in a bunch of source files from Powershell. Here it is so I can quickly look it up again:

Get-ChildItem -include *.cs -recurse | Select-String "string to search for"

The Get-ChildItem is the equivalent of dir in DOS.

-include is the param to tell Get-ChildItem which files to include in its search. In this case all CSharp (C#) files.

-recurse means look in subfolders under this one as well. i.e. the one that I'm running the command from.

| - this is the pipe symbol. The results of the search (a collection of files) is piped into the process that will open each file and search for the string.

Select-String - this is the command that will open a file and search for the "string to search for" string in the file. Each found line will be listed.

No comments:

Post a Comment