Skip to content

Instantly share code, notes, and snippets.

@Nerixyz
Last active February 5, 2023 08:22
Show Gist options
  • Save Nerixyz/4b75418a3fc9be504a281dcd71875c0c to your computer and use it in GitHub Desktop.
Save Nerixyz/4b75418a3fc9be504a281dcd71875c0c to your computer and use it in GitHub Desktop.
Script to set Visual Studio Variables in powershell
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