Quick One-Liner – Search in Files

Today’s one liner is all about how to search file contents in Powershell, recursively.  Bring on the code!

Get-ChildItem -recurse -filter *.cs | Select-String -pattern “SqlConnection” | group path | sort count | select name > sqlconnection.txt

First, let’s just get all C# source files:  Get-ChildItem -recurse -filter *.cs

Second, let’s select all lines with our search string: Select-String -pattern “SqlConnection”

Third, let’s group all the matchinfo objects returned by path: group path

Fourth, let’s sort the grouping objects by count ascending:  sort count

Fifth, let’s select only the name (full file path): select name

Finally, let’s pipe it into a file:  > sqlconnection.txt

Yay!  Easy and simple.  I’m going to turn this into a function that I can have at the ready.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s