Skip to content

Instantly share code, notes, and snippets.

@kfsone
Created January 26, 2023 22:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kfsone/07f98b78d093cb5bdb9af1f73a7772d8 to your computer and use it in GitHub Desktop.
Save kfsone/07f98b78d093cb5bdb9af1f73a7772d8 to your computer and use it in GitHub Desktop.
Powershell macro to enable visual studio developer studio AND save your path
function Enable-VsDevShell ()
{
<#
.Synopsis
Enables Visual Studio developer commands in the current powershell.
.Description
Enables Visual Studio 2019 developer commands for the current powershell session.
Requires Visual Studio 2019 to be installed.
Typically aliased as 'vsdev'.
.Example
PS> Enable-VsDevShell
PS> vsdev # alias
Locates and runs Visual Studio's powershell extension scripts that bring in important
environment variables, helper commands and most importantly PATH changes that allow
the compiler tools to be used.
This isn't done by default because, on Windows, the Visual Studio compiler tool paths
can result in a very unwieldy and inefficient PATH.
#>
if (-Not $IsWindows) {
Write-Output "~~ vsdevshell has no effect except on Windows"
return
}
# Locate the visual studio installer's "where" tool for locating vs paths.
$VsWhere = Join-Path (Join-Path (Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio') Installer) vswhere.exe
if ( ! (Test-Path $VsWhere) ) {
# Not found, can't help.
throw "Visual studio is not installed in a recognized location. Could not find ${vswhere}"
}
# Get the most recent install path (-last 1) from its list of installed locations.
# Specify "-products *" to include build tools
$VisualStudioInstallPath = & $VsWhere -prerelease -latest -products * -property installationPath | select -last 1
if ( ! ($VisualStudioInstallPath) )
{
throw "Visual studio installer located (${vswhere}), but it doesn't think any version of the IDE or Build Tools are installed Please check your installation / contact IT."
}
# Import the devshell module from that install
Import-Module -ea stop (Join-Path (Join-Path (Join-Path $VisualStudioInstallPath Common7) Tools) Microsoft.VisualStudio.DevShell.dll)
# Use the module's "Enter-VsDevShell" to enable it properly
Enter-VsDevShell -VsInstallPath $VisualStudioInstallPath -SkipAutomaticLocation -DevCmdArguments "-arch=amd64 -host_arch=amd64"
if ($env:PATH.length -gt 2000)
{
$Filters = @(
'Linux.bin.ConnectionManagerExe$',
'Microsoft.FSharp.Tools$',
'Microsoft.TeamFoundation.Team Explorer$',
'Microsoft.TestWindow$'
)
$PathParts = $env:PATH -split ';'
$KeepParts = @()
ForEach ($Part in $PathParts)
{
if ($Part -in $KeepParts) { continue }
$Filtered = $false
ForEach ($Filter in $Filters)
{
if ($Part -Match '[\\\/]{0}$' -f $Filter) { $Filtered = $true; break }
}
if (-Not $Filtered) { $KeepParts += $Part }
}
$env:PATH = $KeepParts -Join ';'
Write-Host "-- Path shortened to $($env:PATH.length) characters"
}
}
# Handy alias. -Force so you can run this script multiple times.
New-Alias -Name VsDev -Value Enable-VsDevShell -Force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment