Adding JAVA_HOME to system variable Path via Powershell
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_HOMEc:\tempPS C:\> [Environment]::SetEnvironmentVariable("TEST", "$($Env:JAVA_HOME);c\docs")PS C:\> $env:TESTc:\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
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!
# START POWERSHELL7start-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)