Skip to content

Instantly share code, notes, and snippets.

@drmohundro
Last active May 17, 2016 14:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drmohundro/47b8ec91dd81757b1e43 to your computer and use it in GitHub Desktop.
Save drmohundro/47b8ec91dd81757b1e43 to your computer and use it in GitHub Desktop.
PowerShell script to open a recently used solution in Visual Studio
function Open-MruSolution($sln) {
$mruItems = "HKCU:\Software\Microsoft\VisualStudio\14.0\MRUItems"
$guids = Get-ChildItem $mruitems |
Select-Object -ExpandProperty name |
Foreach-Object { $_.Substring($_.LastIndexOf('\') + 1) }
[array]$mostRecentlyUsedSlns = $guids |
Foreach-Object {
$guid = $_
Get-ChildItem "$mruItems\$guid" |
Select-Object -ExpandProperty Property |
Foreach-Object {
$value = Get-ItemPropertyValue "$mruItems\$guid\Items" -Name $_
if ($value.Contains('.sln')) {
$value.Substring(0, $value.IndexOf('|'))
}
}
}
if ([string]::IsNullOrWhitespace($sln)) {
Write-Host "Recently Used Solutions:`n"
for ($i = 0; $i -lt $mostRecentlyUsedSlns.Count; $i++) {
Write-Host "$($i + 1): $($mostRecentlyUsedSlns[$i])"
}
$toOpen = Read-Host "`nChoose # to open"
if ($toOpen -gt 0 -and $toOpen -le $mostRecentlyUsedSlns.Count) {
Write-Host "Starting $($mostRecentlyUsedSlns[$toOpen - 1])..."
& $mostRecentlyUsedSlns[$toOpen - 1]
}
}
else {
foreach ($mru in $mostRecentlyUsedSlns) {
if ($mru -like "*$sln*") {
Write-Host "Starting $mru..."
& $mru
break
}
}
}
}
Set-Alias o Open-MruSolution
@drmohundro
Copy link
Author

This is a simple script to look in the registry to open a recently used Visual Studio solution by partial name. It is currently hard-coded to VS2013 (see 12.0 reference in $MRUPath).

To use it, just drop it in your $profile and use like o log assuming you have a solution with "log" in the name.

If you don't pass in a $sln argument, it will display all of the recently used solutions and you can then choose which one you wish to open, as seen below:

» o
Recently Used Solutions:

1: C:\dev\foo\foo.sln
2: C:\dev\other\log.sln
3: C:\dev\web\web.sln

Choose # to open:

@drmohundro
Copy link
Author

Updated function for Visual Studio 2015.

@drmohundro
Copy link
Author

Fixed a one-off error.

@drmohundro
Copy link
Author

Fixed a bug where $mostRecentlyUsedSlns wasn't an array when there was only one item returned from the pipeline.

@drmohundro
Copy link
Author

Update to use PowerShell v5 registry function Get-ItemPropertyValue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment