Skip to content

Instantly share code, notes, and snippets.

@beercanx
Created April 20, 2026 13:34
Show Gist options
  • Select an option

  • Save beercanx/79a406488f01d0e09d7c54eacc6dfcab to your computer and use it in GitHub Desktop.

Select an option

Save beercanx/79a406488f01d0e09d7c54eacc6dfcab to your computer and use it in GitHub Desktop.
JAVA_HOME PowerShell 7+ helper functions

JAVA_HOME PowerShell 7+ helper functions

Crude but effective functions I use to manually manage JAVA_HOME on Windows using Powershell 7+, for those times where a project can only use a certain LTS version.

Placed in the Powershell profile file, it just attempts to find the latest version of the specific version and updates your JAVA_HOME evironment varaible.

If your path setup also relies on JAVA_HOME to add Java commands to the path, this also updates those after a refreshenv.

function jdkFinder($version) {

  $env:JAVA_HOME=$(
    Get-ChildItem -Path "$HOME\.jdks","C:\Program Files\Amazon Corretto" -Attributes "Directory"
    | Where-Object -Property Name -Match $version
    | Add-Member -PassThru -MemberType ScriptProperty -Name "Version" -Value {
      $this.Name.Replace("jdk","").Replace("corretto-","") 
    }
    | Sort-Object -Property Version -Descending 
    | Select-Object -First 1
  ).FullName
  
  # Persist the change
  [Environment]::SetEnvironmentVariable("JAVA_HOME", $env:JAVA_HOME, [EnvironmentVariableTarget]::User)

  Write-Host "`nSet JAVA_HOME to ${env:JAVA_HOME}`n"
}
function java25 {
  jdkFinder 25
}
C:\> java25

Set JAVA_HOME to C:\Users\Beer\.jdks\corretto-25.0.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment