如何将PowerShell与Visual Studio命令提示符一起使用?

And*_*y S 116 powershell visual-studio-2010 visual-studio

我现在已经使用Beta 2了一段时间,这让我疯狂,我必须在运行VS2010命令提示符时向cmd.exe发挥作用.我曾经为Visual Studio 2008提供了一个很好的vsvars2008.ps1脚本.任何人都有vsvars2010.ps1或类似的东西吗?

And*_*y S 214

从这里偷偷地偷窃:http://allen-mack.blogspot.com/2008/03/replace-visual-studio-command-prompt.html,我能够让它发挥作用.我将以下内容添加到我的profile.ps1中,并且一切都与世界相符.

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
Run Code Online (Sandbox Code Playgroud)

这已经运行了好几年 - 直到Visual Studio 2015.vcvarsall.bat不再存在.相反,您可以使用vsvars32.bat文件,该文件位于Common7\Tools文件夹中.

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
Run Code Online (Sandbox Code Playgroud)

对于Visual Studio 2017,情况再次发生了变化.vsvars32.bat似乎已被弃用了VsDevCmd.bat.确切的路径可能会有所不同,具体取决于您使用的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
Run Code Online (Sandbox Code Playgroud)

  • `echo $ Profile`可以查看profile.ps1的预期路径,如果您从未创建它的话 (9认同)
  • 请注意,同样的技术适用于Visual Studio 2012.只需将"Microsoft Visual Studio 10.0"更改为"Microsoft Visual Studio 11.0"即可 (6认同)
  • 脚本本身非常有效.但需要注意的是(可能):由于Visual Studio中的程序包管理器控制台本身就是PowerShell主机,因此该脚本也会在那里运行.在您注意到"无需调试运行"或任何其他功能或插件运行时,启动标准Windows控制台在初始化PMC后将无法运行,这似乎不是问题.我解决了这个问题,而不是在"profile.ps1"的答案中保存脚本,而是将其保存到"Microsoft.PowerShell_profile.ps1",以便它只能在"正确的"PowerShell会话中运行. (5认同)
  • 我很欣赏使用环境变量的愿望,但这些变量似乎是由我们试图从中提取变量的批处理文件初始化的.我很乐意看到相反的证据.我有一个干净的Windows 10安装与干净的Visual Studio 2017安装,没有VS150COMNTOOLS环境变量,直到我执行VsDevCmd.bat. (5认同)
  • 当有完美的环境变量(用于VS2015的VS140COMNTOOLS)时,对路径进行硬编码是非常糟糕的做法.这甚至适用于自定义VS安装. (3认同)

Kei*_*ill 25

最简单的选项是运行VS 2010命令提示符,然后启动PowerShell.exe.如果你真的想从你的"home"PowerShell提示符中做到这一点,那么你展示的方法就是你要走的路.我使用了Lee Holmes写的一段脚本:

<#
.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
}
Run Code Online (Sandbox Code Playgroud)

注意:此功能将在即将发布的PowerShell Community Extensions 2.0模块版本中提供.


use*_*702 18

我在这里找到了一个简单的方法:修改快捷方式.

原始快捷方式是这样的:

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

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

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

如果要使其看起来更像PS,请转到快捷方式属性的" 颜色"选项卡,并将红色,绿色和蓝色值分别设置为1,36和86.

截图

  • 我唯一不喜欢这个,是它保持加载多余的cmd.exe进程.除此之外是一个很好的解决方案. (2认同)

Mic*_*ens 16

一个老问题,但值得另一个答案(a)提供VS2013支持; (b)结合前两个答案中的最佳答案; (c)提供功能包装.

这是建立在@ Andy的技术基础之上的(基于Allen Mack的技术,正如Andy指出的那样(反过来建立在Robert Anderson的技术上,正如Allen指出的那样(所有这些都有一个轻微的故障,如本页所示,用户只知道为"我 - - "所以我也考虑到了这一点))).

这是我的最终代码 - 请注意在正则表达式中使用非贪婪量词来处理值中任何可能的嵌入等于.这也恰好简化了代码:单个匹配而不是匹配,然后在Andy的示例中进行拆分,或者匹配,然后在"我 - "的示例中匹配indexof和substrings).

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
}
Run Code Online (Sandbox Code Playgroud)


Tah*_*san 9

Keith已经提到了PowerShell社区扩展(PSCX)及其Invoke-BatchFile命令:

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

我也注意到PSCX也有一个Import-VisualStudioVars功能:

Import-VisualStudioVars -VisualStudioVersion 2013
Run Code Online (Sandbox Code Playgroud)

  • 从PSCX 3.2.0开始,此cmdlet不支持VS 2015.我为它打开了[问题](https://pscx.codeplex.com/workitem/43203). (4认同)

pra*_*kpc 5

代替

  1. 调用批处理脚本
  2. 写下环境变量
  3. 以如此复杂的方式执行修改,使用 Visual Studio 2022(可能还有 2019),您可以使用以下更简单的方法
Import-Module "VS-Directory\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"
Enter-VsDevShell -VsInstallPath "VS-Directory\Community" -DevCmdArguments
Run Code Online (Sandbox Code Playgroud)

但是,如果您希望 VS 默认使用 64 位 MSVC 和环境,则可以使用 arch=64 的 DevCmdArguments

Enter-VsDevShell -VsInstallPath "VS-Directory\Community" -DevCmdArguments '-arch=x64'
Run Code Online (Sandbox Code Playgroud)