Skip to content

Instantly share code, notes, and snippets.

@refactorsaurusrex
Last active February 1, 2024 03:21
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save refactorsaurusrex/2a0d8efd88ceb30eb69488c1b11c7682 to your computer and use it in GitHub Desktop.
Save refactorsaurusrex/2a0d8efd88ceb30eb69488c1b11c7682 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.

Installation (Option 1)

  1. Download this gist as a zip file.
  2. Extract the file to \Documents\WindowsPowerShell\Modules\Open-Solution.
  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>
  6. Profit!

Installation (Option 2)

  1. Run Install-Module -Name WhatsNew. https://github.com/refactorsaurusrex/whats-new
  2. Type sln.
  3. Profit!
@{
RootModule = 'Open-Solution.psm1'
ModuleVersion = '0.1.0'
GUID = '42f8cad0-c32a-4ccd-9401-de4bdbafdd65'
Author = 'Nick Spreitzer'
FunctionsToExport = @('Open-Solution')
AliasesToExport = @('sln')
}
function Open-Solution() {
param(
[string]$RootDirectory = ''
)
$solutions = Get-ChildItem -recurse -path "$RootDirectory*.sln"
if ($solutions.Count -eq 1) {
& $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 solution. Which one do you want to open?"
$solutions | ForEach-Object { write-host " - $($_.FullName)" }
}
}
Set-Alias sln Open-Solution
@Adam--
Copy link

Adam-- commented May 4, 2021

I stumbled on this today and just wanted to say thanks for creating this! I usually type *.sln or **\*.sln and tab, but to be honest that's a bit of finger gymnastics and this is much simpler (though a bit slower to run).

@thwyster
Copy link

thwyster commented Feb 1, 2024

Hello, firstly I would like to thank you immensely for this module, I have been using it for some time, and it really saves your time, I recently formatted my machine, and I came to install the module again, I tried using powershell and powershel7, but the command shows an error:

image

I solved my problem because I saw that it was possible to install the module via chocolatey, but I think it was interesting to note that using the pwsh command didn't work...

@refactorsaurusrex
Copy link
Author

refactorsaurusrex commented Feb 1, 2024 via email

@thwyster
Copy link

thwyster commented Feb 1, 2024

Oh sorry my ctrl v is wrong, i type the complete row:
Install-Module -Name WhatsNew. https://github.com/refactorsaurusrex/whats-new

https://github.com/refactorsaurusrex/whats-new shouldn't be in charge

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