SubZeno SubZeno - 3 months ago 13
C# Question

How to permanently set Environment Variables for the Visual Studio command line

For my project I need to use the Visual Studio Development Tools as additional command line features, for instance to create dlls with the tlbimp utility.

According to the Microsoft documentation, I need to properly configure my environment variables for the Visual Studio Command Line, by calling the

vsvars32.bat
.


The
vsvars32.bat
file sets the appropriate environment variables to enable command-line builds. (More info: https://msdn.microsoft.com/en-us//library/1700bbwd.aspx)


What looks annoying in my case, it's that these features are temporary available in my PowerShell prompt. Every time I need to re-run that batch file. Is there a clever way to make them persistent?

Answer

You can configure or modify (if it already exists) your Powershell profile in order to automatically invoke the vsvars32.bat batch file:

function Invoke-Batchfile ($file) {
    $cmd = "`"$file`" & set"
    cmd /c $cmd | Foreach-Object {
    $p, $v = $_.split('=')
        Set-Item -path env:$p -value $v
    }
}

Invoke-Batchfile "$env:VSCOMNTOOLS\vsvars32.bat"

Take into account that you first need to define as well an environment variable pointing to your Visual Studio installation path, according to the used version:

  • For Visual Studio 15 Update 3 (v.14.0):

$env:VSCOMNTOOLS = "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools"

  • For Visual Studio 13. (v.12.0):

$env:VSCOMNTOOLS = "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools"

  • Etc...

Now, everytime you open your Poweshell prompt, the Visual Studio development tools' features will be available.