Skip to content

Instantly share code, notes, and snippets.

@Adam--
Forked from refactorsaurusrex/Open-Solution.psd1
Last active January 17, 2023 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 = ''
)
if ($Depth -eq -1) {
$solutions = Get-ChildItem -recurse -path "$RootDirectory*.sln"
}
else {
$solutions = Get-ChildItem -recurse -path "$RootDirectory*.sln" -depth $Depth
}
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"
# Provides a clickable link in Windows Terminal
$solutions | ForEach-Object { write-host "file://localhost/$($_.FullName -replace '\\', '/')" }
write-host
}
}
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