Windows classics: user PATH and computer PATH

Posted: (EET/GMT+2)

 

Some Windows concepts never age, and the PATH environment variable is one of these. Whether you're a developer or an administrator, understanding the difference between User PATH and System (Computer) PATH can save hours of troubleshooting.

Firstly, remember the main rule: computer PATHs take precedence over user PATHs, and naturally, each user can have a different user PATH.

In PowerShell, it's very easy to view the combined (computer + user, in this order) PATH with just:

$env:PATH

This will output a semicolon-separated lists of paths. Windows will search these from left to right, top to bottom.

Now, how do you separate the computer path from the user path? Since this operating can be done with C# and .NET using the System.Environment class, you can also do it with PowerShell. Here's how:

[Environment]::GetEnvironmentVariable("Path", "Machine")
[Environment]::GetEnvironmentVariable("Path", "User")

Notice the names "Machine" for computer paths, and "User" for user paths.

Windows stores the path settings in the system registry:

  • The user path is stored under HKEY_CURRENT_USER\Environment\Path
  • The computer (aka system, machine) path is stored under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path.

If you want to modify the machine path, you can use again a bit of .NET code as follows in a PowerShell prompt:

[Environment]::SetEnvironmentVariable
     ("Path", $env:Path + “;C:\MyNewPath”, [System.EnvironmentVariableTarget]::Machine)

Some of you might say that why do it in a complicated way, when you can just say:

setx PATH "$env:PATH;C:\MyNewPath"

Well, this works, but there's a gotcha: the "setx" command will truncate your machine path to a maximum of 1024 characters. Modern paths are often longer (especially on developer machines), so care is needed. A good idea is to backup your existing path (machine + user) to a Notepad text file, for instance, and only then modify the path.

If you want to play it safe, use Windows Control Panel. From there, go to "System", then "Environment Variables".

Finally, a reminder on applying the changes. Windows operating system provides each process a copy of the environment variables at process start. Thus, if you modify the path, it doesn't immediately affect already-running processes. For example, if you change the PATH while a command prompt is already open, that prompt won't see the new value; only newly started processes will.