Skip to content

Instantly share code, notes, and snippets.

@Adam--
Forked from refactorsaurusrex/Open-Solution.psd1
Last active May 10, 2024 12:37
Show Gist options
  • Save Adam--/b90e7ba769ef1f5166682f97854a4755 to your computer and use it in GitHub Desktop.
Save Adam--/b90e7ba769ef1f5166682f97854a4755 to your computer and use it in GitHub Desktop.
PowerShell function to open the first Visual Studio solution file found within the current directory.

What is this thing?

Rather than navigating around Windows explorer, looking for that pesky .sln file you want to open, why not just open in from your command line? This module allows you to type sln from your PowerShell terminal window to open the first .sln file found within the current directory or any of its children. This also works with atsln (Atmel Studio) solution files.

Installation

  1. Download this gist as a zip file.
  2. Extract the file to \Documents\WindowsPowerShell\Modules\Open-Solution for PowerShell or \Documents\PowerShell\Modules\Open-Solution for PowerShell Core.
  3. Restart PowerShell.
  4. Navigate to a directory that contains a Visual Studio solution file and type sln.
  5. Alternatively, type sln <PATH_TO_DIRECTORY_CONTAINING_VS_SOLUTION_FILE_AT_ANY_DEPTH>
@{
RootModule = 'Open-Solution.psm1'
ModuleVersion = '0.2.0'
GUID = '0d320efd-ae51-49bf-a0b2-3f20ae76d6d0'
Author = 'Original: Nick Spreitzer; Modified: Adam Anderson'
FunctionsToExport = @('Open-Solution', 'Open-AtmelSolution')
AliasesToExport = @('sln', 'atsln')
}
function Open-Solution {
param(
[int]$Depth = -1,
[string]$RootDirectory = ''
)
$excludeFolders = @("obj", "bin", ".*")
$solutions = @()
write-host "Searching for solution files..."
if ($Depth -eq -1) {
$directories = Get-ChildItem -Recurse -Path $RootDirectory -Directory
if ($directories.Count -gt 100) {
write-host "Searching $($directories.Count) folders, this may take a moment."
}
$solutions = Get-ChildItem -Recurse -Path "$RootDirectory*.sln" -Exclude $excludeFolders
}
else {
$directories = Get-ChildItem -Recurse -Path $RootDirectory -Directory -Depth $Depth
if ($directories.Count -gt 100) {
write-host "Searching $($directories.Count) folders, this may take a moment."
}
$solutions = Get-ChildItem -Recurse -Path "$RootDirectory*.sln" -Depth $Depth -Exclude $excludeFolders
}
if ($solutions.Count -eq 1) {
write-host "I found one solution file! Opening it now..."
write-host $solutions[0].FullName
& $solutions.FullName
}
elseif ($solutions.Count -eq 0) {
write-host "I couldn't find any solution files here!"
write-host "Check the root directory and depth parameters. The following folders are excluded: $excludeFolders"
}
elseif ($solutions.Count -gt 1) {
write-host "I found $($solutions.Count) solutions. Which one do you want to open?`n"
# Provides a clickable link in Windows Terminal
$solutions | ForEach-Object { write-host "file://localhost/$($_.FullName -replace '\\', '/')" }
}
}
function Open-AtmelSolution {
param(
[string]$RootDirectory = ''
)
$solutions = Get-ChildItem -recurse -path "$RootDirectory*.atsln"
if ($solutions.Count -eq 1) {
write-host $solutions[0].FullName
& $solutions.FullName
}
elseif ($solutions.Count -eq 0) {
write-host "I couldn't find any solution files here!"
}
elseif ($solutions.Count -gt 1) {
write-host "I found more than one solution. Which one do you want to open?`n"
$solutions | ForEach-Object { write-host $_.FullName }
write-host
}
}
Set-Alias sln Open-Solution
Set-Alias atsln Open-AtmelSolution
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment