Skip to content

Instantly share code, notes, and snippets.

@jeserkin
Last active October 16, 2023 09:04
Show Gist options
  • Save jeserkin/96b1d1977cfd018e7bcf09491518bfcb to your computer and use it in GitHub Desktop.
Save jeserkin/96b1d1977cfd018e7bcf09491518bfcb to your computer and use it in GitHub Desktop.
Java version switcher
# In case you have different JDK versions on PC and wish to switch between them easily
# Current pattern is JAVA\d+_HOME
# So make sure you have ENV variables like JAVA17_HOME, etc.
if (-not $env:JAVA_HOME) { $env:JAVA_HOME = [Environment]::GetEnvironmentVariable('JAVA_HOME', 'User') }
Write-Host "(Current: $env:JAVA_HOME)"
# Alternative to OGV
#$javaVersion = Read-Host "Select JAVA version: "
#$javaVersion = $javaVersion.Trim()
#$selection = (ls env: | ? name -eq "JAVA$($javaVersion)_HOME").name
$selection = (ls env: | ? name -Match 'JAVA\d+_HOME').name | ogv -OutputMode single -Title "List of possible JAVA versions:"
Write-Host "You chose: $selection" -NoNewLine
if ($selection) {
[Environment]::SetEnvironmentVariable('JAVA_HOME', (ls env: | ? name -eq $selection).value, 'User')
$env:JAVA_HOME = [Environment]::GetEnvironmentVariable('JAVA_HOME', 'User')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment