Skip to content

Instantly share code, notes, and snippets.

@PhilMurwin
Created February 3, 2025 18:35
Show Gist options
  • Save PhilMurwin/cbba063cbe291691a4a8953e03c72310 to your computer and use it in GitHub Desktop.
Save PhilMurwin/cbba063cbe291691a4a8953e03c72310 to your computer and use it in GitHub Desktop.
Script to list sln files in a directory and start user selected sln from powershell
## Open sln in Visual Studio from the containing folder
function Start-VisualStudio([string]$folder)
{
# Set folder to current directory if not specified
if ([string]::IsNullOrWhiteSpace($folder))
{
$folder = $(Get-Location).Path
}
# Ensure folder path ends with a backslash
if ($folder -notmatch '\\$')
{
$folder += '\'
}
# Get all sln files located in the directory tree
# Use .NET API for faster file searching
$SlnFiles = [System.IO.Directory]::EnumerateFiles($folder, "*.sln", [System.IO.SearchOption]::AllDirectories) | ForEach-Object { Get-Item $_ }
# Convert to an array for counting
$SlnFiles = @($SlnFiles)
# If only one sln file exists, open it immediately
if ($SlnFiles.Count -eq 1)
{
Invoke-Item $SlnFiles[0].FullName
return
}
# Handle multiple sln files
if ($SlnFiles.Count -gt 1)
{
Write-Host "`nMore than one vs solution file found!"
# Create a formatted table with headers
$slnOptions = for($i = 0; $i -lt $SlnFiles.Count; $i++)
{
[PSCustomObject]@{
'Opt' = "${i}: "
'Solution Name' = $SlnFiles[$i].Name
'Solution Path' = $SlnFiles[$i].Directory
}
}
# Display the table
$slnOptions | Format-Table -AutoSize
# Get user selection
$input = Read-Host "Select a Solution to startup (q to quit)"
# Validate user input
if ($input -match '^\d+$' -and [int]$input -ge 0 -and [int]$input -lt $SlnFiles.Count)
{
Invoke-Item $SlnFiles[$input].FullName
}
else
{
Write-Host "Invalid selection, quitting."
}
}
}
# Short alias function
function vs {
param(
[string]$folder
)
Start-VisualStudio -folder $folder
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment