Friday, 26 July 2013

Searching all local drives for files using Windows Powershell

There's often a "forensic" side to software development. Unfortunately source control and continuous integration aren't going to be in place on all projects that you work on and, from time to time, you'll need to track down some elusive source code. I remember once helping to track down the source code for an important project on an old hard drive in the company owner's garage!

At the moment, I'm helping to pin down local copies of source code on some developers' PCs and needed a quick way of searching for all Visual Studio solutions on their hard drives. The following Powershell script searches recursively through all folders on all local drives for files with the ".sln" extension and outputs to a file.

Look at the script on github if you can't see the code in-line above.

The main challenge was getting the list of available drives. "Get-PSDrive -PSProvider FileSystem" will get you all local drives, but includes CD / DVD drives which might not be available. Filtering this list further using "Where-Object -Property Free" will restrict the list to available drives.