Reputation: 195
I want to set a variable say JAVA_HOME to dynamically assume the value of another variable say JAVA_HOME_x64..such that whenever JAVA_HOME_x64 changes and a new terminal session is created JAVA_HOME gets assigned the value of whatever is value of JAVA_HOME_x64.
In powershell , I do following...
[System.Environment]::SetEnvironmentVariable('JAVA_HOME', '%JAVA_HOME_x64%', 'Machine');
[System.Environment]::SetEnvironmentVariable('JAVA_HOME_x64', 'c:\myjava', 'Machine');
[System.Environment]::GetEnvironmentVariable('JAVA_HOME', 'Machine') ==> returns %JAVA_HOME_x64%
When I open a new command prompt and type set(or echo %JAVA_HOME%), it shows me value of JAVA_HOME as %JAVA_HOME_x64% and not c:\myjava.
However if i go to UI(as below) and manually edit(delete and recreate) JAVA_HOME and reset its value to %JAVA_HOME_x64%, and then when a launch a new command prompt/terminal session it now shows value of JAVA_HOME as c:\myjava..Why so ? Any ideas? How to achieve the same behaviour as is in UI via PowerShell commands.
After some investigation i realize that, registry key(in Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment) created via above powershell command line for the said variable is of type REG_SZ, while the one created via UI is REG_EXPAND_SZ. This maybe the clue
Upvotes: 3
Views: 639
Reputation: 195
The following worked..
Set-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' 'JAVA_HOME' '%JAVA_HOME_x64%' -Type 'ExpandString'
[System.Environment]::SetEnvironmentVariable('JAVA_HOME_x64','c:\program files\Zulu\jre','Machine')
Upvotes: 2