refactorsaurusrex
7/20/2017 - 5:26 PM

PowerShell function to open the first Visual Studio solution file found within the current directory.

PowerShell function to open the first Visual Studio solution file found within the current directory.

@{
  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

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!