Why is setx path not working?

戏子无情 提交于 2021-02-18 06:27:32

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/35347451/why-is-setx-path-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!