Adding JAVA_HOME to system variable Path via Powershell

33,935

Solution 1

After I installed JAVA from oracle website I set

 [System.Environment]::SetEnvironmentVariable("JAVA_HOME", "C:\Program Files\Java\jdk-13.0.1")

To set java in the path I did,

 [System.Environment]::SetEnvironmentVariable("Path", [System.Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::Machine) + ";$($env:JAVA_HOME)\bin")

Worked like a charm!

If you want to have it load up automatically when you open powershell you can do,

New-Item $profile -Type File -Force

and open in notepad like

notepad.exe $profile

and paste in

 [System.Environment]::SetEnvironmentVariable("JAVA_HOME", "C:\Program Files\Java\jdk-13.0.1")
 [System.Environment]::SetEnvironmentVariable("Path", [System.Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::Machine) + ";$($env:JAVA_HOME)\bin")

You can close the notepad now! Next, you want to allow powershell to run the ps script so don't forget to grant unrestricted access to running the profile script on load by,

 Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

Java should now be loaded after you reopen powershell. Thats it!

Solution 2

First you can use the following syntax to reach then environememnt variable in PowerShell :

$env:Path

So in your case you can write :

[Environment]::SetEnvironmentVariable("Path", [Environment]::GetEnvironmentVariable('Path', [EnvironmentVariableTarget]::Machine) + "$($Env:JAVA_HOME)\bin", [EnvironmentVariableTarget]::Machine)

Here is an example :

PS C:\> [Environment]::SetEnvironmentVariable("JAVA_HOME", "c:\temp")
PS C:\> $env:JAVA_HOME
c:\temp

PS C:\> [Environment]::SetEnvironmentVariable("TEST", "$($Env:JAVA_HOME);c\docs")

PS C:\> $env:TEST
c:\temp;c\docs

Is this what you are looking for ?

# example of adding a path to PATH
[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";$($Env:JAVA_HOME)\bin", "User")

Be carefull : $Env:Path is the merge of System Path and User Path. Depending on the user who executes this command, the resulting Path will contain different entries (both user profile ones and original system ones). If you really want to change only system ones you should use :

$oldSysPath = (Get-Itemproperty -path 'hklm:\system\currentcontrolset\control\session manager\environment' -Name Path).Path
$newSysPath = $oldSysPath + ";$($Env:JAVA_HOME)\bin"

Set-ItemProperty -path 'hklm:\system\currentcontrolset\control\session manager\environment' -Name Path -Value $newSysPath 

Solution 3

# START POWERSHELL7
start-process "c:\Program Files\PowerShell\7\pwsh.exe" -verb runas
# ADD "JAVA_HOME"
[System.Environment]::SetEnvironmentVariable("JAVA_HOME", "c:\opt\jdk-15", [System.EnvironmentVariableTarget]::Machine)
[System.Environment]::GetEnvironmentVariable("JAVA_HOME", [System.EnvironmentVariableTarget]::Machine)
# ADD "bin += path"
[System.Environment]::SetEnvironmentVariable("Path", "%JAVA_HOME%\bin;" + [Environment]::GetEnvironmentVariable('Path', [EnvironmentVariableTarget]::Machine), [System.EnvironmentVariableTarget]::Machine)
[System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine)
Share:
33,935
muhihsan
Author by

muhihsan

Updated on June 15, 2022

Comments

  • muhihsan
    muhihsan less than a minute

    I'm trying to add %JAVA_HOME%\bin to Path environment variables via Powershell script.

    The JAVA_HOME variable itself is pointing to C:\Program Files\Java\jdk1.8.0_172.

    When I added %JAVA_HOME%\bin manually from the Environment Variable window

    Environment Variable

    Then call this line of code from Powershell to get value of Path variable

    [Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::Machine)
    

    It seems like the result from executing line above converts %JAVA_HOME%\bin to the actual path that I've defined which is C:\Program Files\Java\jdk1.8.0_172.

    The output look like this

    ...;C:\Program Files\nodejs;C:\Program Files\Java\jdk1.8.0_172\bin;
    

    But when I added %JAVA_HOME%\bin via Powershell script with the code below

    [Environment]::SetEnvironmentVariable("Path", [Environment]::GetEnvironmentVariable('Path', [EnvironmentVariableTarget]::Machine) + "%JAVA_HOME%\bin", [EnvironmentVariableTarget]::Machine)
    

    Then run GetEnvironmentVariable function again, the output is different than when I added the path through the environment variable window. It doesn't convert %JAVA_HOME%\bin to the actual path.

    The output looks like this

    ....;C:\Program Files\nodejs\;C:\Program Files\Java\jdk1.8.0_172\bin;%JAVA_HOME%\bin
    

    Is this expected? Or is there something that I am missing?

    I can actually just append the real path to Path variable directly, but I want to make use of JAVA_HOME variable so the path will be in 1 location.

  • muhihsan
    muhihsan about 4 years
    Thanks for the answer, but this will just add the path directly to the TEST environment variable. I actually want to add a dynamic path from powershell by adding %JAVA_HOME% (like how we can do it from the environment variable window) so if one day the path for JAVA_HOME change, it will be reflected in others that uses %JAVA_HOME% (Like Path). Is this possible?
  • davidmpaz
    davidmpaz 21 days
    I must add the in order for me to work I needed to change the order in PATH due to priority. Instead adding: $($env:JAVA_HOME)\bin at the end, I did it at the start. Thanks!