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.