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
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.
Add to the Windows PATH environment variable To add to the PATH, append a semicolon and a new path on the end of the long path string. We can use PowerShell to check whether the path we want to add is already in the existing path.
In PowerShell, we use the gcm command that is short for get-command . It returns all the commands in the machine. We use gcm with two parameters; the first is -All that shows all the instances of the command in the current machine, and the second parameter is the command name. In our case, the command name is java .
Click Environment variables. Under User variables, click New. Enter JAVA_HOME as the variable name. Enter the path to the JDK as the variable value.
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 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With