Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lkurzyniec
Last active January 3, 2024 11:46
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lkurzyniec/da61b9b5cec71730c3b89cdfe31f37ff to your computer and use it in GitHub Desktop.
Save lkurzyniec/da61b9b5cec71730c3b89cdfe31f37ff to your computer and use it in GitHub Desktop.
tf version switcher
@echo off
IF "%~1"=="" (
call ECHO No version specified. Call with '?' parameter to get a list of available versions
) ELSE (
IF "%~1"=="?" (
call ECHO Available versions:
call ECHO -------------------
call dir %PROGRAMDATA%\chocolatey\lib /b | findstr /i "terraform"
) ELSE (
call ECHO %~1>"%UserProfile%/.terraform-version"
call ECHO Selected version:
call more "%UserProfile%/.terraform-version"
call ECHO Performing reload of PowerShell profile. After that, you are ready to go!
call ECHO -------------------
call powershell & $profile
)
)
Create file:
C:\Users\[user]\.terraform-version
with:
0.11.14
choco install terraform --version 1.1.5 -my
choco install terraform --version 1.1.9 -my
# in your user profile file add below code, to get path to your user profile location execute: $PROFILE
function Set-TfEnv {
$tfenvfile = "$($env:UserProfile)/.terraform-version"
if (!(Test-Path $tfenvfile)) {
Remove-Alias -Name tf
Write-Host "No .terraform-version file"
return
}
$terraformVersion = Get-Content -Path $tfenvfile
$terraformVersion = $terraformVersion -replace "terraform.", ""
Set-Alias -Name tf -Value "C:\ProgramData\chocolatey\lib\terraform.$($terraformVersion)\tools\terraform.exe" -Scope Global
}
Set-TfEnv
@lkurzyniec
Copy link
Author

lkurzyniec commented Jan 15, 2021

Remember to use tf instead of terraform.

image

@p0onage
Copy link

p0onage commented Mar 12, 2021

I also added a function to list installed terraform versions

function tfversions {
    Get-ChildItem -Path 'C:\ProgramData\chocolatey\lib\' -Filter "terraform.*"
}

@lkurzyniec
Copy link
Author

lkurzyniec commented Mar 12, 2021

good idea! I refreshed !tf-switch.bat file and added dir command with findstr pipe.

@lkurzyniec
Copy link
Author

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