我如何使用Visual Studio命令提示符使用PowerShell?

我已经使用Beta 2了一段时间了,这一直是驱使我疯狂,我不得不在运行VS2010命令提示符时踢cmd.exe。 我曾经为Visual Studio 2008有一个很好的vsvars2008.ps1脚本。任何人都有一个vsvars2010.ps1或类似的东西?

从这里偷窃: http : //allen-mack.blogspot.com/2008/03/replace-visual-studio-command-prompt.html ,我能够得到这个工作。 我将以下内容添加到了我的个人资料。

pushd 'c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC' cmd /c "vcvarsall.bat&set" | foreach { if ($_ -match "=") { $v = $_.split("="); set-item -force -path "ENV:\$($v[0])" -value "$($v[1])" } } popd write-host "`nVisual Studio 2010 Command Prompt variables set." -ForegroundColor Yellow 

这一直运行良好多年 – 直到Visual Studio 2015.vcvarsall.bat不再存在。 相反,您可以使用位于Common7 \ Tools文件夹中的vsvars32.bat文件。

 pushd 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools' cmd /c "vsvars32.bat&set" | foreach { if ($_ -match "=") { $v = $_.split("="); set-item -force -path "ENV:\$($v[0])" -value "$($v[1])" } } popd write-host "`nVisual Studio 2015 Command Prompt variables set." -ForegroundColor Yellow 

对于Visual Studio 2017而言,情况再次发生了变化VsDevCmd.bat似乎已经被VsDevCmd.bat 。 确切的path可能会有所不同,具体取决于您正在使用的Visual Studio 2017的版本。

 pushd "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools" cmd /c "VsDevCmd.bat&set" | foreach { if ($_ -match "=") { $v = $_.split("="); set-item -force -path "ENV:\$($v[0])" -value "$($v[1])" } } popd Write-Host "`nVisual Studio 2017 Command Prompt variables set." -ForegroundColor Yellow 

最简单的select是运行VS 2010命令提示符,然后启动PowerShell.exe。 如果你真的想从你的“家”PowerShell提示符做到这一点,你展示的方法是要走的路。 我用了一个李福尔摩斯写的一个脚本:

 <# .SYNOPSIS Invokes the specified batch file and retains any environment variable changes it makes. .DESCRIPTION Invoke the specified batch file (and parameters), but also propagate any environment variable changes back to the PowerShell environment that called it. .PARAMETER Path Path to a .bat or .cmd file. .PARAMETER Parameters Parameters to pass to the batch file. .EXAMPLE C:\PS> Invoke-BatchFile "$env:VS90COMNTOOLS\..\..\vc\vcvarsall.bat" Invokes the vcvarsall.bat file to set up a 32-bit dev environment. All environment variable changes it makes will be propagated to the current PowerShell session. .EXAMPLE C:\PS> Invoke-BatchFile "$env:VS90COMNTOOLS\..\..\vc\vcvarsall.bat" amd64 Invokes the vcvarsall.bat file to set up a 64-bit dev environment. All environment variable changes it makes will be propagated to the current PowerShell session. .NOTES Author: Lee Holmes #> function Invoke-BatchFile { param([string]$Path, [string]$Parameters) $tempFile = [IO.Path]::GetTempFileName() ## Store the output of cmd.exe. We also ask cmd.exe to output ## the environment table after the batch file completes cmd.exe /c " `"$Path`" $Parameters && set > `"$tempFile`" " ## Go through the environment variables in the temp file. ## For each of them, set the variable in our local environment. Get-Content $tempFile | Foreach-Object { if ($_ -match "^(.*?)=(.*)$") { Set-Content "env:\$($matches[1])" $matches[2] } } Remove-Item $tempFile } 

注意:此function将在即将推出的基于PowerShell社区扩展 2.0模块的版本中提供。

一个老问题,但值得另一个答案(一)提供VS2013 suppport; (b)结合前面两个答案中的最佳答案; 和(c)提供一个函数包装器。

这是build立在@安迪的技术(它是build立在艾伦·麦克的技术上,正如安迪所指出的那样(这反过来又build立在罗伯特·安德森的技术上,正如艾伦指出的那样(所有这些都有一个小故障, – “,所以我也考虑到了这一点)))。

这里是我最后的代码 – 注意在正则expression式中使用非贪婪量词来处理任何可能的embedded等于值。 这也恰好简化了代码:单个匹配而不是匹配,然后按照Andy的例子进行拆分,或匹配indexof和substrings,如“me – ”的例子)。

 function Set-VsCmd { param( [parameter(Mandatory, HelpMessage="Enter VS version as 2010, 2012, or 2013")] [ValidateSet(2010,2012,2013)] [int]$version ) $VS_VERSION = @{ 2010 = "10.0"; 2012 = "11.0"; 2013 = "12.0" } $targetDir = "c:\Program Files (x86)\Microsoft Visual Studio $($VS_VERSION[$version])\VC" if (!(Test-Path (Join-Path $targetDir "vcvarsall.bat"))) { "Error: Visual Studio $version not installed" return } pushd $targetDir cmd /c "vcvarsall.bat&set" | foreach { if ($_ -match "(.*?)=(.*)") { Set-Item -force -path "ENV:\$($matches[1])" -value "$($matches[2])" } } popd write-host "`nVisual Studio $version Command Prompt variables set." -ForegroundColor Yellow } 

我在这里find了一个简单的方法:修改快捷方式。

原来的快捷方式是这样的:

 %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat"" 

在最后一个报价之前添加& powershell ,如下所示:

 %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat" & powershell" 

如果您想让它看起来更像PS,请转到快捷方式属性的“ 颜色”选项卡,并将红色,绿色和蓝色值分别设置为1,36和86。

截图

Keith已经提到PowerShell社区扩展(PSCX)和它的Invoke-BatchFile命令:

 Invoke-BatchFile "${env:ProgramFiles(x86)}\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" 

我也注意到PSCX也有一个Import-VisualStudioVars函数:

 Import-VisualStudioVars -VisualStudioVersion 2013 

荣获安迪S的回答。 我一直在使用他的解决scheme,但今天遇到了一个问题。 任何具有等号的值在等号处被截断。 例如,我有:

 JAVA_TOOL_OPTIONS=-Duser.home=C:\Users\Me 

但是我的PS会话报道:

 PS C:\> $env:JAVA_TOOL_OPTIONS -Duser.home 

我通过修改我的configuration文件脚本来解决以下问题:

 pushd 'c:\Program Files (x86)\Microsoft Visual Studio 11.0\VC' cmd /c "vcvarsall.bat&set" | foreach { if ($_ -match "=") { $i = $_.indexof("=") $k = $_.substring(0, $i) $v = $_.substring($i + 1) set-item -force -path "ENV:\$k" -value "$v" } } popd 

我喜欢把命令传递给一个子shell,像这样:

 cmd /c "`"${env:VS140COMNTOOLS}vsvars32.bat`" && <someCommand>" 

或者,

 cmd /c "`"${env:VS140COMNTOOLS}..\..\VC\vcvarsall.bat`" amd64 && <someCommand> && <someOtherCommand>"