7

I am unable to use the cl command in PowerShell.

I tried to add the following command to my PowerShell profile to exec vcbuildtools.bat, but PowerShell does not recognize cl command on PowerShell?

&"C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat"

OS: Windows 10

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
KiYugadgeter
  • 3,796
  • 7
  • 34
  • 74

5 Answers5

6

Just to be clear I'm addressing the asker's issue that cl is not in the PATH even after running this in PowerShell

&"C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat"

I think this boils down to the issue that batch file can't export variables to PowerShell (also related: this question), as you've found out with vcbuildtools.bat. I think it's because PowerShell invokes a cmd.exe subshell to execute the batch file which changes the environment in the subshell but the changes don't propagate to the parent shell i.e. PowerShell.


Solution 1

One way is to use the fact that subshell inherits the environment from the parent shell. So if you run this in PowerShell, the environment set by the batch file is preserved

cmd.exe /k "C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat" `& powershell

Take note of `&. The character has to be escaped because it has a special meaning in PowerShell.


Solution 2

The Pscx module has an Import-VisualStudioVars function which imports environment variables for Visual Studio. An example usage is

Import-VisualStudioVars 2015 amd64

if you're using VS/BuildTools 2015 and compiling 64-bit programs. You can use Pop-EnvironmentBlock to revert the changes. See man Import-VisualStudioVars -full for more information.

Alternatively, Pscx also has an Invoke-BatchFile function that retains environment changes by a batch file. An example usage

Invoke-BatchFile "C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat"

See man Invoke-Batchfile -full for more information.

Notes

  • To download the up-to-date version of Pscx from PowerShell gallery, you will need PowerShellGet which is shipped with PowerShell 5 and is available as a downloadable installer for PowerShell 3 and 4.
  • For those with PowerShell 1 and 2, older versions of Pscx is available on Codeplex.
doubleDown
  • 8,048
  • 1
  • 32
  • 48
  • thanks: This solved my issue of compiling C from powershell, i was able to run: cmd.exe /k "C:\Program Files (x86)\Microsoft Visual St udio 14.0\Common7\Tools\vsvars32.bat" `& powershell AND then compile my c code with header files – schmoopy Jan 26 '18 at 18:08
3

You can use the following function to invoke a cmd.exe shell script (batch file) and persist its environment variables:

function Invoke-CmdScript {
  param(
    [String] $scriptName
  )
  $cmdLine = """$scriptName"" $args & set"
  & $env:SystemRoot\system32\cmd.exe /c $cmdLine |
  Select-String '^([^=]*)=(.*)$' | ForEach-Object {
    $varName = $_.Matches[0].Groups[1].Value
    $varValue = $_.Matches[0].Groups[2].Value
    Set-Item Env:$varName $varValue
  }
}

Add this function to your PowerShell profile, and run the batch file using the function:

Invoke-CmdScript "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\vsvars32.bat"
Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
  • Great answer and thank you. This worked for me while "solution 1" by IT-Rafa did not, which would halt execution of my powershell script with no error after calling the BAT. – aatwo Jan 13 '20 at 16:21
2

Fortunately, VS 2019 Community now has a Developer PowerShell for VS 2019 command.

enter image description here

The actual command, if you want to see the properties for the shortcut, is rather verbose.

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -noe -c "&{Import-Module """C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell 14bbfab9}"

Anyway, I am using this and it adds the right cl.exe to my path, but there is an odd message after running it:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\ostream(750): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
.\hey.cpp(4): note: see reference to function template instantiation 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const char *)' being compiled
Spencer Williams
  • 821
  • 10
  • 20
  • Thank you so much! This was the only thing that worked for me in this page. I do not have admin. The only problem to take into account is that if you want to run this command in PowerShell you need to rewrite it to: `&"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe" -noe -c "&{Import-Module 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll'; Enter-VsDevShell 14bbfab9}"` No error for me afterwards.... – CaribeGirl Oct 14 '21 at 16:38
  • An odd thing is that this "Developer Powershell" silently defaults to use the x86 version of cl.exe. – Elad Maimoni May 27 '22 at 09:13
1

Another option from PowerShell gallery: posh-vs

Makes Visual Studio command line tools available in PowerShell. Supports Visual Studio 2017 and 2015.

IT-Rafa
  • 21
  • 3
1

I also encountered the same problem, type cmd.exe and you'll change control to command line.

PowerShell example

If you want to go back to PowerShell, no problem. Just write exit. As simple as it sounds

segev608
  • 23
  • 4