33

I'm using powershell to "grep" my source code for a particular string. If the string is in the file, I would like the name of the file, not the line of code that contains the string.

I would also like the name of the file, just once, not listed for as many times as the file exists.

I'm currently using:

gci . -include "*.sql" -recurse | select-string -pattern 'someInterestingString'

Now I understand that the output of select-string is some sort of ojbect, and what I'm seeing in the console is, i'm guessing, the ToString() of that object. I assume that I could use format-table to control the output of the select-string, and I suppose sort to get distinct values only.

but that's a lot of guessing.

3 Answers 3

68

I don't think I completely understand what you're trying to do. If you want the output grouped by file, you can pipe into Format-Table with the -GroupBy parameter:

gci . -include "*.sql" -recurse `
    | select-string -pattern 'someInterestingString' `
    | Format-Table -GroupBy Path

If you want to get only the names of the files that match without any other info, you can use Select-Object with the -Unique parameter:

gci . -include "*.sql" -recurse `
    | select-string -pattern 'someInterestingString' `
    | Select-Object -Unique Path

If you're interested in only the file name, regardless whether the name itself appears multiple times in your hierarchy, then you can select the Filename property instead.


Note: The Get-Member cmdlet is a great help in figuring out what properties exist on an object:

gci . -include "*.sql" -recurse `
    | select-string -pattern 'someInterestingString' `
    | Get-Member

You can also use its alias gm instead.

Sign up to request clarification or add additional context in comments.

3 Comments

This is exactly the kind of detailed answer that I was looking for. Thanks!
In order for Select-Object to return a string instead of an object with a single Path property, use | Select-Object -ExpandProperty Path -Unique
| Select-Object -ExpandProperty Filename -Unique will give the filename as output
18

When I'm doing this I just use the -List parameter - yes it does display the line of code but you only get one line per file (no matter how many matches there are):

PS> Get-ChildItem . -r *.cs | Select-String XmlNode -list

Commands\SnapinHelp\CmdletInfo.cs:27:        public List<XmlNode> InputTypes;
Commands\SnapinHelp\GetSnapinHelpCommand.cs:124:            WriteXmlNodeList(c...
Commands\SnapinHelp\ParameterInfo.cs:73:        XmlNode FindNode(XmlDocument doc)
Commands\Xml\XmlCommandBase.cs:65:            RegisterInputType<XmlNode>(Proce...

If you want the path:

PS> Get-ChildItem . -r *.cs | Select-String XmlNode -list | 
    Format-Table Path

Path
--------
C:\Users\Keith\Pscx\Src\PscxSnapin\Commands\SnapinHelp\CmdletInfo.cs
C:\Users\Keith\Pscx\Src\PscxSnapin\Commands\SnapinHelp\GetSnapinHelpCommand.cs
C:\Users\Keith\Pscx\Src\PscxSnapin\Commands\SnapinHelp\ParameterInfo.cs
C:\Users\Keith\Pscx\Src\PscxSnapin\Commands\Xml\XmlCommandBase.cs

Or if you really only want the filename:

PS> Get-ChildItem . -r *.cs | Select-String XmlNode -list | 
    Format-Table Filename

Filename
--------
CmdletInfo.cs
GetSnapinHelpCommand.cs
ParameterInfo.cs
XmlCommandBase.cs

3 Comments

Much simpler than the (more generic) solution above... +1.
Only down side to this approach is that if binary files are in the search path the first line maybe the whole file.
@Tolga To remove binaries, just use -exclude *.dll, *.pdb, *.cache, *.exe in get-childitem, or any other binary extensions that appear.
4

I found it easier to do

(...|select-string "search").Path

2 Comments

This works, but it will return the file's path each time the searched for string is found in the file.
Despite the above observation, this simple, short, and elegant. Try i!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.