Reputation: 9201
A script needs to add a program to the Windows PATH. Another script needs to remove that same program from the Windows PATH. For compatibility issues, the script needs to work on most if not all flavors of Windows.
Which registry key (or keys) consistently store the PATH on a wide range of Windows machine types?
For example, on my Windows 10 Home Edition laptop, the PATH is stored in the Path
property of the following key:
HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
But another user informs me this key is not available on his or her Windows machine.
So what is the complete list of key location possibilities?
Note that the scripts are targeting the keys directly because the changes to the PATH must persist after the end of runtime. Other approaches seem to only temporarily change the PATH while the program is running.
Upvotes: 4
Views: 15799
Reputation: 21468
From PowerShell, persistently set the PATH
environment by using the following method:
[Environment]::SetEnvironmentVariable( 'VARIABLE_NAME', 'VALUE', [EnvironmentVariableTarget]::Machine )
To remove the environment variable, set the environment variable value to $null
:
[Environment]::SetEnvironmentVariable( 'VARIABLE_NAME', $null, [EnvironmentVariableTarget]::Machine )
As for why your users are missing that registry key? That sounds like a larger problem because HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
is where the System Environment Variables are stored and retrieved from. It's been that way since XP, and the documentation states this as far back as .NET Framework 2.0.
If that key is missing on someone's machine, I'd wager the user is either not looking in the right place, or some sort of malware could be the cause.
If you want to set the environment variable at the process level, as asked in the comments, you can use the $env:
variable to read and set environment variables at the process level:
$env:VARIABLE_NAME = 'VALUE'
Upvotes: 4