Setting up Visual Studio environment variables from PowerShell [duplicate] Setting up Visual Studio environment variables from PowerShell [duplicate] powershell powershell

Setting up Visual Studio environment variables from PowerShell [duplicate]


You should use InvokeEnvironment script to do that. Check its man page:

Invoke-Environment <path_to_>vsvars32.bat

You can furhter generalize this by determining OS bits and crafting the vsvars<OsBits>.bat.

Example:

PS C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools> $env:INCLUDE -eq $nullPS C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools> $truePS C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools> Invoke-Environment .\vsvars32.batPS C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools> $env:INCLUDEC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE;C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE;C:\Program Files (x86)\Windows Kits\10\include\10.0.10586.0\ucrt;C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um;C:\Program Files (x86)\Windows Kits\10\include\10.0.10586.0\shared;C:\Program Files (x86)\Windows Kits\10\include\10.0.10586.0\um;C:\Program Files (x86)\Windows Kits\10\include\10.0.10586.0\winrt;


Now there is a powershell module posh-vs that will install the proper stuff in your $profile.

PS> Install-module posh-vsPS> Install-PoshVs


I don't have Visual Studio at hand, but the batch scripts most likely just set variables for the current session. Running them from PowerShell won't do you any good, because they'll be launched in a child CMD process and change the process environment of that process, but not of the parent (PowerShell) process.

I suspect you need to translate the variable definitions to PowerShell, e.g.

set PATH=%PATH%;C:\some\whereset FOO=bar

becomes

$env:Path += ';C:\some\where'$env:FOO = 'bar'

Write the translated definitions to a .ps1 file and dot-source that file in your PowerShell session:

. C:\path\to\vcvars.ps1