Skip to content

Instantly share code, notes, and snippets.

@johnwildes
Last active April 13, 2025 12:47
Show Gist options
  • Save johnwildes/e25057e4d9705ef633ce0a21cf5f56ec to your computer and use it in GitHub Desktop.
Save johnwildes/e25057e4d9705ef633ce0a21cf5f56ec to your computer and use it in GitHub Desktop.
Terraform Update / Via Powershell
# This script updates Terraform to the latest version available from HashiCorp.
# It checks the current version, downloads the latest version if necessary,
# and replaces the existing binary. It also backs up the current version.
# Ensure you have the required permissions to run this script and modify the installation directory.
# Created by: John Wildes with the helpf of GitHub Copilot
# Date: 2025-04-03
# Description: Update Terraform to the latest version
# Add a parameter for the installation directory
param (
[string]$InstallDir = "C:\Tools",
[switch]$Install = $false
)
# Update the script to use the parameter for the installation directory
$terraformExe = Join-Path $InstallDir "terraform.exe"
# Check if Terraform is installed
if (Test-Path $terraformExe) {
# Get the current Terraform version
$currentVersion = & $terraformExe -version | Select-String -Pattern "Terraform v([\d\.]+)" | ForEach-Object { $_.Matches.Groups[1].Value }
Write-Host "Current Terraform version: $currentVersion"
} else {
Write-Host "Terraform is not installed in $InstallDir."
$currentVersion = $null
# If Install switch is not provided and Terraform doesn't exist, exit
if (-not $Install) {
Write-Host "Use -Install switch to install Terraform if it's not found."
return
}
Write-Host "Will install the latest version of Terraform..."
}
# Fetch the latest Terraform version from HashiCorp's releases
$latestVersion = Invoke-WebRequest -Uri "https://releases.hashicorp.com/terraform/" -UseBasicParsing | Select-String -Pattern "terraform/([\d\.]+)/" | ForEach-Object { $_.Matches.Groups[1].Value } | Sort-Object -Descending | Select-Object -First 1
Write-Host "Latest Terraform version: $latestVersion"
# Compare versions if Terraform is already installed
if ($currentVersion -eq $latestVersion) {
Write-Host "You already have the latest version of Terraform installed."
return
}
# Create installation directory if it doesn't exist
if (-not (Test-Path $InstallDir)) {
Write-Host "Creating installation directory: $InstallDir"
New-Item -Path $InstallDir -ItemType Directory -Force | Out-Null
}
# Prepare the download URL and file paths
$downloadUrl = "https://releases.hashicorp.com/terraform/$latestVersion/terraform_${latestVersion}_windows_amd64.zip"
$zipFile = Join-Path $InstallDir "terraform_${latestVersion}.zip"
# Download the latest Terraform release
Write-Host "Downloading Terraform $latestVersion..."
Invoke-WebRequest -Uri $downloadUrl -OutFile $zipFile
# Backup the current version if it exists
if (Test-Path $terraformExe) {
$backupName = "terraform.$((Get-Date).ToString('ddMM'))"
Rename-Item -Path $terraformExe -NewName $backupName
Write-Host "Backed up current Terraform to $backupName"
}
# Extract the new Terraform binary
Write-Host "Extracting Terraform $latestVersion..."
Expand-Archive -Path $zipFile -DestinationPath $InstallDir -Force
# Clean up the downloaded zip file
Remove-Item $zipFile
Write-Host "Terraform $latestVersion installed successfully in $InstallDir."
@johnwildes
Copy link
Author

I know that you can get terraform installed on windows with winget, and this script is extremely brittle and only focused on the windows OS. However, I'm old. I have a folder called Tools and this is added to my path statement. A hold over from when I ran MS-DOS as a kid. So I gin'd up this script using copilot to help keep terraform up to date without having to manually go and do it myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment