Skip to content

Instantly share code, notes, and snippets.

@milnak
Created June 13, 2026 05:47
Show Gist options
  • Select an option

  • Save milnak/75e4c83d5c7d02cf0522044d323f914a to your computer and use it in GitHub Desktop.

Select an option

Save milnak/75e4c83d5c7d02cf0522044d323f914a to your computer and use it in GitHub Desktop.
Install Visual Studio Build Tools
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# https://community.chocolatey.org/packages/visualstudio2017buildtools
$vsProduct = @{
PackageName = 'visualstudio2017buildtools'
ApplicationName = 'Microsoft Visual Studio Build Tools 2017'
Url = 'https://download.visualstudio.microsoft.com/download/pr/85966aef-8515-43ec-a8d5-8d95fd7c9998/16c64a1f7d15fd92c001dde4d16eb1b7e07daee0cad17425adbe3f476d5bc656/vs_BuildTools.exe'
Checksum = '16C64A1F7D15FD92C001DDE4D16EB1B7E07DAEE0CAD17425ADBE3F476D5BC656'
ChecksumType = 'SHA256'
InstallerTechnology = 'WillowVS2017OrLater'
Product = 'BuildTools'
VisualStudioYear = '2017'
Preview = $false
}
try {
$tempRoot = [IO.Path]::GetTempPath()
$randomDir = [IO.Path]::GetRandomFileName()
$TempPath = (New-Item -ItemType Directory -Path (Join-Path $tempRoot $randomDir)).FullName
if (-not (Test-Path -LiteralPath $TempPath -PathType Container)) {
throw "Failed to create temporary directory at '$TempPath'."
}
$installerPath = Join-Path $TempPath "$($vsProduct.PackageName).exe"
Invoke-WebRequest -Uri $vsProduct.Url -OutFile $installerPath
if (-not (Test-Path -LiteralPath $installerPath -PathType Leaf)) {
throw "Installer was not downloaded to '$installerPath'."
}
$actualHash = (Get-FileHash -Algorithm $vsProduct.ChecksumType -Path $installerPath).Hash
$expectedHash = $vsProduct.Checksum.ToUpperInvariant()
if ($actualHash -ne $expectedHash) {
throw "Checksum mismatch. Expected '$expectedHash' but got '$actualHash'."
}
Write-Host "Download succeeded and checksum verification passed."
Write-Host "Installer path: $installerPath"
}
catch {
Write-Error "Failed to prepare installer: $($_.Exception.Message)"
throw
}
$installerArguments = `
"--add", `
"Microsoft.VisualStudio.Workload.VCTools", `
"--includeRecommended", `
"--includeOptional"
try {
if (-not (Test-Path -LiteralPath $installerPath -PathType Leaf)) {
throw "Installer path is invalid: '$installerPath'."
}
& $installerPath @installerArguments
$installerExitCode = $LASTEXITCODE
if ($installerExitCode -eq 3010) {
Write-Warning "Installer completed successfully but requires a reboot (exit code 3010)."
}
elseif ($installerExitCode -ne 0) {
throw "Installer failed with exit code $installerExitCode."
}
Write-Host "Installer completed successfully (exit code $installerExitCode)."
}
catch {
Write-Error "Installer execution failed: $($_.Exception.Message)"
throw
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment