Top

When you install visual studio on windows, the installer adds an application entry for starting powershell in developer mode. Visual studio devtools like link, cmake etcetra can be used from an instance of powershell launched with this entry. However, powershell takes way longer to start when using this option (around a second). This article describes how to speed it up.

When powershell is launched using this option, it imports a cmdlet module (a dll) and executes a cmdlet defined by this module (`Enter-VSDevshell`). This cmdlet analyzes your visual studio installation and sets some variables, and is slow.

You can speed it up by launching the developer powershell, capturing the snapshot of environment variables in this shell, and reloading this snapshot in your init script. This is what the following powershell code does:

set-alias -name filter where-object set-alias -name map foreach-object $VS_ID = '2d6a363d' $devenvcache = $(join-path -Path $(get-item $PROFILE).Directory -child __CACHED_DEV_ENV.csv) function Rerecord-DevEnv { Import-Module 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll' Enter-VsDevshell $VS_ID -StartInPath $(get-location) ls env: | map {select-object -InputObject $_ -Property Key,Value} | export-csv -path $devenvcache Import-DevEnv } function Import-DevEnv { if (! $(test-path -type leaf -path $devenvcache)) { echo 'Dev environment cache not found, run ''Rerecord-DevEnv'' to generate it' return } $exclusions = $( 'LOGONSERVER', 'TERM_SESSION_ID', 'TERMINAL_EMULATOR' ) import-csv -path $devenvcache | filter {!$exclusions.Contains($_.key.ToUpper())} | map {set-item -path $(join-path -path env: -child $_.key) -value $_.value} } Import-Devenv

The rest of the article describes this code.

$VS_ID = '2d6a363d'

Visual studio installation id, you can find it by checking the command line of visual studio developer shell application launcher.

$devenvcache = $(join-path -Path $(get-item $PROFILE).Directory -child __CACHED_DEV_ENV.csv)

Path of environment cache file. $PROFILE is the path of init script ($HOME\Documents\Powershell\...ps1). We create the cache file in the same directory.

function Rerecord-DevEnv { Import-Module 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll' Enter-VsDevshell $VS_ID -StartInPath $(get-location) ls env: | map {select-object -InputObject $_ -Property Key,Value} | export-csv -path $devenvcache Import-DevEnv }

This command enters the devshell and exports the environment to the cache file. Run it whenever you install something.

function Import-DevEnv { if (! $(test-path -type leaf -path $devenvcache)) { echo 'Dev environment cache not found, run ''Rerecord-DevEnv'' to generate it' return } $exclusions = $( 'LOGONSERVER', 'TERM_SESSION_ID', 'TERMINAL_EMULATOR' ) import-csv -path $devenvcache | filter {!$exclusions.Contains($_.key.ToUpper())} | map {set-item -path $(join-path -path env: -child $_.key) -value $_.value} }

Load the dev environment, skipping session specific variables.

Import-DevEnv