- There are three different sets of environment variables
- Process (temporary)
- User (permanent)
- Machine (aka. system, permanent)
- Windows
Path
is an Windows environment variable with the namePath
.
User PATH Machine/System PATH
\ /
\ /
v v
+-----------------------------------------------------+
| MainProcess |
| |
| PATH = Process PATH |
| (initially copy of User and Machine PATH) |
| |
+-----------------------------------------------------+
|
|
v
+-----------------------------------------------------+
| ChildProcess |
| |
| PATH = Process PATH |
| (initially copy of MainProcess PATH) |
+-----------------------------------------------------+
Thanks for the diagram tool, textik.com
- These are used for setting temporary environment variables. They are destroyed when the process terminates.
- When process starts, it gets either (a) Copy of the Process PATH from its parent or (b) Copy of union of User and System PATH.
- In Python, just read
os.environ['PATH']
. - This is the same as reading value of
$Env:Path
(powershell) orPATH
(cmd).
- In Python, just edit
os.environ['PATH']
.
- These values are permanent.
- When process starts, the Process PATH receives all items from User PATH.
When [Environment]::GetEnvironmentVariable(...)
is called with EnvironmentVariableTarget.User
as the target, then (source)
- Variable is retrieved from the
HKEY_CURRENT_USER\Environment
key of the local computer's registry
When [Environment]::SetEnvironmentVariable(...)
is called with EnvironmentVariableTarget.User
as the target, then
- Variable is stored in the
HKEY_CURRENT_USER\Environment
key of the local computer's registry. This means that the environment variable is stored permanently. (source) - Other applications are notified of the set operation by a Windows
WM_SETTINGCHANGE
message. (source) - Variable is copied to instances of File Explorer that are running as the current user
- Environment variable is then inherited by any new processes that the user launches from File Explorer.
- Microsoft recommends that the length is kept under 2048 characters. The hard limit is 32766 characters. (source)
- Note: Setting User environment variable does not necessarily update the Process environment variables.
- These values are permanent.
- When process starts, the Process PATH receives all items from System/Machine PATH.
- Need admin rights to edit these.
When [Environment]::GetEnvironmentVariable(...)
is called with EnvironmentVariableTarget.Machine
as the target, then
- Variable is retrieved from the
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
[*1] key of the local computer's registry.
When [Environment]::SetEnvironmentVariable(...)
is called with EnvironmentVariableTarget.Machine
as the target, then
- Variable is stored to the
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
[*1] key of the local computer's registry. This means that the environment variable is stored permanently. (source) - Other applications are notified of the set operation by a Windows
WM_SETTINGCHANGE
message. (source) - Variable is copied to instances of File Explorer that are running as the current user
- Environment variable is then inherited by any new processes that the user launches from File Explorer.
- Microsoft recommends that the length is kept under 2048 characters. The hard limit is 32766 characters. (source)
- Note: Setting Sytem/Machine environment variable does not necessarily update the Process environment variables.
[*1] HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
according to this source, HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment
according to this source. These might be the same thing.