Why is setx path not working?

17,564

In Windows, each process gets a copy of the environment which is essentially a snapshot of the global environment at the time the process was started. Changes to the global environment while the process is running are not propagated back to the process' own copy of the environment.

To answer the actual question, setx does modify the user environment (or the system one if run with /M), but the changes are not immediately visible in the process that executed setx, in this case cmd.exe. If you open a new command prompt after running setx, you'll see the changes in that instance of cmd.exe.

This is explicitly noted in the setx /? help:

On a local system, variables created or modified by this tool will be available in future command windows but not in the current CMD.exe command window.

To effect the same changes in both the global environment, and the one of the current process, you need to run both setx andset.

Share:
17,564
johny why
Author by

johny why

Updated on July 23, 2022

Comments

  • johny why
    johny why almost 2 years

    Can someone explain this result?

    After setting path, it did not change. This was run in an Administrator command-line:

    C:\Windows\system32>setx path "C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;D:\Program Files (x86)\Microsoft VS Code\bin"
    
    SUCCESS: Specified value was saved.
    
    C:\Windows\system32>path
    PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;D:\Program Files (x86)\Microsoft VS Code\bin;D:\Program Files (x86)\metapad36;D:\Program Files (x86)\metapad36" /M
    

    I've read that %PATH% = PATH variable for machine + PATH variable for user. Am I seeing the machine path + Admin's path?

    Have looked at other articles on the topic, but still confused.

    Should I clear the user paths, so there's no duplication?

    update: Re the tip that "variables created or modified by this tool will be available in future command windows" i open a non-admin window and enter:

    >path
    PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;D:\Program Files (x86)\Microsoft VS Code\bin;;D:\Program Files (x86)\metapad36;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;D:\Program Files (x86)\Microsoft VS Code\bin
    

    The path is repeated twice. Ok, then at same prompt I setx the path without the repeat, and without /M:

    >setx path "C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;D:\Program Files (x86)\Microsoft VS Code\bin"
    
    SUCCESS: Specified value was saved.
    

    Apparently saved in current user environment.

    Then i open a new non-admin command window, and:

    >path
    PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;D:\Program Files (x86)\Microsoft VS Code\bin;;D:\Program Files (x86)\metapad36;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;D:\Program Files (x86)\Microsoft VS Code\bin`
    

    It has not changed. Why?

  • Yar
    Yar almost 3 years
    I have a similar case, but in Set Environment Variables, in shells run as Administrator and in registry (both current user and system-wide variables) PATH is ok, because I set it this way in the registry editor. Even after reboot though $Env:PATH or echo %path% show path with duplicates. Setx executes successfully but nothing changes for subsequent instances of shells, set only changes PATH for current shell session. How do I get my PATHs in sync?
  • dxiv
    dxiv almost 3 years
    @Yar That's hard to guess without any details. First thing to check would be the user and machine environments after each change, see here for example. If that doesn't match what you expect you should post a separate question with the exact steps to reproduce the problem.