Solarian Programmer

My programming ramblings

Using the Visual Studio Developer Command Prompt from the Windows Terminal

Posted on October 25, 2020 by Paul

In this article, I will show you how to use the Visual Studio command line compiler from the Windows Terminal. I will assume that you’ve already installed Visual Studio 2019 on your machine. In my case, I have installed the Community edition with the Desktop development with C++ workload. By default, the installer will create a few links for using the development tools from the classical Windows Command Prompt and we are going to copy some settings from this to the Windows Terminal. My second assumption is that you’ve also installed the Windows Terminal, if this is not the case just open Microsoft Store, search for Windows Terminal and install it.

If you’ve ever played with Windows Terminal you already know why it is nicer to use than the classical Command Prompt, a few examples: tabs, vertical and horizontal splits, a resizable window, zoom in and out, proper copy paste to and from the Terminal, background colors and a lot of other possible customizations.

From the Windows Start menu, find Visual Studio 2019x64 Native Tools Command Prompt for VS 2019 and right click on it, chose More and select Open file location. This will open a folder in Windows Explorer, typically this folder will contain a couple of links to cmd.exe customized for development usage. Find the one named x64 Native Tools Command Prompt for VS 2019, right click on it and select Properties. Select the Shortcut tab, if it is not already selected, and copy the content of Target in a new text file. As an example, this is what mine contains:

1 %comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"

We need to do some modifications to the above line in order to be able to use it: remove %compspec%, escape the double quotes and replace each backward slash from the path with two forward slashes. You should end up with something like this (if you have the Professional or Entreprise editions of Visual Studio change the line accordingly):

1 /k \"C://Program Files (x86)//Microsoft Visual Studio//2019//Community//VC//Auxiliary//Build//vcvars64.bat\"

Next, open the Windows Terminal settings, this is a JSON file that can be customized. You can access the Settings by clicking on the downward arrow from the Windows Terminal menu. The Settings file should contain a few predefined profiles. For our particular case, easiest approach is to duplicate the settings for cmd.exe profile, e.g. in my case:

1 {
2     // Make changes here to the cmd.exe profile.
3     "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
4     "name": "Command Prompt",
5     "commandline": "cmd.exe",
6     "hidden": false
7 },

Duplicate (copy and paste) the equivalent of the above for your case in the settings.json file.

Please note the guid variable, this is a unique identifier for each profile and we need to generate one. From the Windows Start menu open a classical x64 Native Tools Command Prompt for VS 2019 and write:

1 guidgen

The above should start an app named Create GUID. Select option 4 Registry Format, press New GUID and than Copy, replace the guid value from the duplicated profile with the value that you just copied (hint you can paste the copied guid).

Optionally, change the name value from the Settings file to something more descriptive e.g.: x64 Native Tools Command Command Prompt for VS 2019

Next, let’s append to the end of the commandline the modified path to vcvars64 from above.

You should end up with something like this:

1 {
2     // Make changes here to the x64  Native Tools Command Command Prompt for VS 2019 profile.
3     "guid": "{FA148184-C8B3-465E-A67C-4542E087E3C6}",
4     "name": "x64 Native Tools Command Prompt for VS 2019",
5     "commandline": "cmd.exe /k \"C://Program Files (x86)//Microsoft Visual Studio//2019//Community//VC//Auxiliary//Build//vcvars64.bat\"",
6     "hidden": false
7 },

Optionally, you can start in a particular directory instead of the default one by setting the value of startingDirectory, e.g:

1     "startingDirectory": "C:/DEV"

Save the settings.json file and reopen Windows Terminal. If there was any syntax error in settings.json you should see some warning message in the Windows Terminal, otherwise it should start normally.

Let’s try our new profile - from the drop down menu of the Windows Terminal (down arrow) select your new custom entry e.g. x64 Native Tools Command Command Prompt for VS 2019 if you followed this tutorial. As an example, this is what I see on my machine if I open the x64 Native … profile and invoke the cl.exe compiler:

 1 **********************************************************************
 2 ** Visual Studio 2019 Developer Command Prompt v16.7.6
 3 ** Copyright (c) 2020 Microsoft Corporation
 4 **********************************************************************
 5 [vcvarsall.bat] Environment initialized for: 'x64'
 6 
 7 C:\>cl
 8 Microsoft (R) C/C++ Optimizing Compiler Version 19.27.29112 for x64
 9 Copyright (C) Microsoft Corporation.  All rights reserved.
10 
11 usage: cl [ option... ] filename... [ /link linkoption... ]
12 
13 C:\>

If you prefer PowerShell to the Command Prompt for command line development, the procedure is similar. Duplicate the existing PowerShell profile from the Windows Terminal settings and replace the guid with a new value. Next, replace the value of commandline with:

1 powershell.exe cmd.exe /k \\\"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat\\\" `& powershell

As an example, this is how it looks on my machine (you may need to adapt it a bit for your particular case):

1 {
2     // Make changes here to the x64 Native Tools PowerShell for VS 2019 profile.
3     "guid": "{444226FA-04DE-4EC4-B529-0C00EAD16A82}",
4     "name": "x64 Native Tools PowerShell for VS 2019",
5     "commandline": "powershell.exe cmd.exe /k \\\"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat\\\" `& powershell",
6     "startingDirectory": "C:/DEV",
7     "hidden": false
8 },

At this point, you can use the Visual Studio developer tools from Windows Terminal. If you need to learn more about how to configure Windows Terminal check the documentation. For more info about using the command line for development check the Microsoft documentation.


Show Comments