Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the cl command on PowerShell?

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

like image 864
KiYugadgeter Avatar asked Jan 30 '17 09:01

KiYugadgeter


People also ask

How do you use CL?

CL:A can be used as a building, a house, a trophy, a statue, and similar concepts. Just like an English pronoun, you must first identify what you are talking about (a house for example). Then you can use the classifier to show where it is located, or what is near it.

What is CLS command in PowerShell?

Description. The Clear-Host function removes all text from the current display, including commands and output that might have accumulated. When complete, it displays the command prompt. You can use the function name or its alias, cls .

How do I use Microsoft PowerShell commands?

Using PowerShell.exe Otherwise, the session is the same as any session that is started in the Windows PowerShell console. To start a Windows PowerShell session in a Command Prompt window, type PowerShell . A PS prefix is added to the command prompt to indicate that you are in a Windows PowerShell session.


1 Answers

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.
like image 76
doubleDown Avatar answered Oct 25 '22 08:10

doubleDown