## Monday, July 30, 2012

### Launching Visual Studio solutions made easy with PowerShell

In my previous post on “Launching Apps with PowerShell Functions”, I showed you how to launch a visual studio solution.  That was very easy I guess because we used the start command and provided full file path with a .sln extension. It started visual studio to open this file.

I am going to extend this solution to solve one of my pain points with Visual Studio Start Page.  On VS start page there is a section for “Recent Projects” which lists recently opened visual studio solutions.  In that section you can pin a solution which you like VS2010 to remember.  It is a great feature to open your most used visual studio solutions.  However, when you get into creating a lot of visual studio projects for development, testing some random code and production.  Over a time this list gets unmanageable and it won’t remember too many projects.  You don’t want to pin all these test projects and bad projects you have created because they will conflict with your frequently pinned production solutions.  Those test solutions might be good for future testing purposes.  Again you don’t want them to go away either.  So I wanted to list all the solutions that I have created in my two or three directories over time and use start command to open it.  There could be more solutions elsewhere but I am not interested in those.  So here is my solution:

I have create a script which will search two or three interested directories like C:\BadSourceCode, C:\ProductionSourceCode, C:\Testing etc for .sln file and list them.  Then I will provide a number to launch a particular visual studio solution I am interested in.  This way I don’t have to wait in the morning to wake up my visual studio from sleep and search and find the solution I want to open.  Ouch!! That is too much of work for me when I am half asleep.  Instead I just want to fire up PowerShell and supply visual studio solution number that I want to open and bang it just works while I sit back and relax.

So here is the script.  You can put the entire script in your profile or store it in a location and create powershell function to run that script.

Write-Host " "
Write-Host "Listing all visual studio solutions" -ForegroundColor yellow
Write-Host " "
Add-Type @'
public class solution
{
public int id;
public string name;
public string fullname;
}
'@
$tempSolutions = Get-ChildItem C:\Testing,C:\Production,C:\BadSourceCodes -Recurse -Include *.sln | select Name, FullName$si = New-Object solution
$st = [Type]$si.GetType()
$base = [System.Collections.Generic.List1]$qt = $base.MakeGenericType(@($st))
$so = [Activator]::CreateInstance($qt)
for($i = 0;$i -lt $tempSolutions.Count;$i++)
{
$obj = New-Object solution$obj.id = $i$obj.name = $tempSolutions[$i].Name
$obj.fullname =$tempSolutions[$i].FullName$so.Add($obj) }$so | Format-Table -AutoSize
$choice = Read-Host "Enter a number to open a vs solution or letter x to exit: " if($choice -eq "x"){
Write-Host -ForegroundColor red "exited selection" -BackgroundColor black
}
else
{
Write-Host -ForegroundColor Yellow "Opening VS solution:" $so[$choice].name
Start-Process $so[$choice].fullname
}

The above script will recursively list all .sln files and then create a custom object for each file and then add it to a list object.  To launch a particular solution, we provide a number to get that object from a list and use the fullname property of the object to the start command to open that solution inside visual studio 2010.

I have created this little function and inside this function I provide the path to the script file for launching solutions.

function get-vssolutions {
C:\scripts\OpenVSsolutions.ps1
}

PS C:\> get-vssolutions

Enter a number to open a vs solution or type letter x to exit: