Skip to content

Instantly share code, notes, and snippets.

@xt0rted
Created July 22, 2022 20:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xt0rted/a2798b530aab9ff3ec3584ec90e1c452 to your computer and use it in GitHub Desktop.
Save xt0rted/a2798b530aab9ff3ec3584ec90e1c452 to your computer and use it in GitHub Desktop.
Shortcut to open the first .sln found in Visual Studio

Shortcut to open in Visual Studio

Running Open-VisualStudio, or the vs alias, in any folder will open the first *.sln found in the stable version of Visual Studio. Using the -pre flag will use the prerelease version that's installed.

function Open-VisualStudio {
Param(
[switch] $prerelease
)
$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
if (!(Test-Path $vswhere)) {
Write-Host "vswhere must be installed to use this command" -ForegroundColor Red
return
}
$path = Convert-Path .
$sln = Get-ChildItem -Path $path\* -Include *.sln | Select-Object -First 1
if (!$sln) {
Write-Host "No .sln file found" -ForegroundColor Red
return
}
if ($prerelease) {
$devenv = & $vswhere -latest -prerelease -property productPath | Select-Object -First 1
} else {
$devenv = & $vswhere -latest -property productPath | Select-Object -First 1
}
if (!$devenv) {
Write-Host "Visual Studio was not found" -ForegroundColor Red
return
}
& $devenv $sln
}
New-Alias -Name vs -Value Open-VisualStudio -Scope Script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment