Powershell

Searching for some string in files using Powershell

Posted on

There was a conditional mapping change that I need to remove from the code repository, and unfortunately it’s not a .Net solution so Ctrl+Shft+F (Find in files) is not an option :|

Window search (built in) is not being helpful (as usual) and I’m too lazy to create a small script to search for the conditional mapping in the files.

Suddenly, I remembered about powershell, that should be able to do the job ;)

So in the end, after 15 minutes searching for the good stuffs at the web:

http://www.computerperformance.co.uk/ezine/ezine125.htm

http://technet.microsoft.com/en-us/library/dd347677.aspx

Tada, got the nice one line script below:

get-childitem C:\folders\ -recurse -include *.* | select-string 'some text in file' | group-object path | Format-Table -AutoSize -Wrap -Property Count, Group, Name

What it does:

– Search for all files under C:\folders\ + recursively

– look for ‘some text in file’

– group the results by the path name

– format the result into table with autosize, wrap options.

– display the properties in order -> Count, Group, Name

Nice ;)