Last active
February 5, 2023 08:22
-
-
Save Nerixyz/4b75418a3fc9be504a281dcd71875c0c to your computer and use it in GitHub Desktop.
Script to set Visual Studio Variables in powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-CommandExists { | |
param ($command) | |
$oldPreference = $ErrorActionPreference | |
$ErrorActionPreference = 'stop' | |
try { | |
if(Get-Command $command) { | |
return $true | |
} | |
} catch { | |
return $false | |
} finally { | |
$ErrorActionPreference = $oldPreference | |
} | |
} | |
$defaultPath = "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build"; | |
if (Test-CommandExists vswhere) { | |
pushd "$(vswhere -latest -property installationPath)\VC\Auxiliary\Build" | |
Write-Host "Found through vswhere"; | |
} elseif (Test-CommandExists Get-VSSetupInstance) { | |
pushd "$((Get-VSSetupInstance).InstallationPath)\VC\Auxiliary\Build" | |
Write-Host "Found through Get-VSSetupInstance"; | |
} elseif (Test-CommandExists ($defaultPath + "\vcvars64.bat")) { | |
pushd $defaultPath | |
Write-Host "Found default community installation"; | |
} else { | |
Write-Host "Neither vswhere.exe, Get-VSSetupInstance, or default community installation found. Consider either adding vswhere.exe to your PATH or running 'Install-Module VSSetup'." | |
exit 1 | |
} | |
cmd /c "vcvars64.bat&set" | | |
foreach { | |
if ($_ -match "=") { | |
$v = $_.split("=", 2); set-item -force -path "ENV:\$($v[0])" -value "$($v[1])" | |
} | |
} | |
popd | |
Write-Host "Visual Studio Command Prompt variables set." -ForegroundColor Yellow |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment