This one’s straight off of How I can use PowerShell with the Visual Studio Command Prompt? and it works a treat.
So I amend the $profile file with the following (updated to include VS 2015)
function Set-VsCmd
{
param(
[parameter(Mandatory=$true, HelpMessage="Enter VS version as 2010, 2012, 2013, 2015")]
[ValidateSet(2010,2012,2013,2015)]
[int]$version
)
$VS_VERSION = @{ 2010 = "10.0"; 2012 = "11.0"; 2013 = "12.0"; 2015 = "14.0" }
if($version -eq 2015)
{
$targetDir = "c:\Program Files (x86)\Microsoft Visual Studio $($VS_VERSION[$version])\Common7\Tools"
$vcvars = "VsMSBuildCmd.bat"
}
else
{
$targetDir = "c:\Program Files (x86)\Microsoft Visual Studio $($VS_VERSION[$version])\VC"
$vcvars = "vcvarsall.bat"
}
if (!(Test-Path (Join-Path $targetDir $vcvars))) {
"Error: Visual Studio $version not installed"
return
}
pushd $targetDir
cmd /c $vcvars + "&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
}
The previous version (non-VS 2015) is listed below in case it’s still needed
function Set-VsCmd
{
param(
[parameter(Mandatory=$true, 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
}
Another user on the same question of stackoverlow also put forward the idea of simply changing the shortcut that Visual Studio supply to add the & powershell, like this
%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat" & powershell"