Ever had the frustration of having to open up ‘Developer PowerShell for VS’ for every small C++ program you write, and then having to cd
to where it’s saved, only to accidentally close it after a run and curse yourself why you did that? Fear not, for there’s a simple solution!
The first step is to use the shortcut to extract some useful information. Find the location of the shortcut by searching for ‘Developer powershell’ from the start menu and go to ‘Open file location’ on the pane on the right. Open properties and check under Target
. On mine, it looks something like this.
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -noe -c "&{Import-Module """long-path\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell some_seemingly_random_string}"
The bits we’re interested in are,
Import-Module """long_path\Microsoft.VisualStudio.DevShell.dll"""
Enter-VsDevShell some_seemingly_random_string
You’ll need to add this to your Powershell profile so that it can be invoked whenever you need it.
# Warning! This will overwrite your existing profile, if you have any
PS > New-Item -Force -Path $PROFILE # create your powershell profile
PS > notepad $PROFILE
Add the following lines
# You can name the functions as you please
function LoadVsTools {
Import-Module "long_path\Microsoft.VisualStudio.DevShell.dll";
Enter-VsDevShell some_seemingly_random_string -SkipAutomaticLocation | Out-Null; # Supress banner
}
function UnloadVsTools {
Remove-Module Microsoft.VisualStudio.DevShell;
}
And you’re good to go!
PS > LoadVsTools
PS > cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29333 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
usage: cl [ option... ] filename... [ /link linkoption... ]