为什么 setx 路径不起作用?

Why is setx path not working?

谁能解释一下这个结果?

设置路径后,没有变化。这是 运行 在管理员命令行中:

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

我读到 %PATH% = 机器的 PATH 变量 + 用户的 PATH 变量。我看到的是机器路径 + 管理员路径吗?

看过该主题的其他文章,但仍然一头雾水。

我应该清除用户路径,以免重复吗?

更新:关于"variables created or modified by this tool will be available in future command windows"的提示,我打开一个非管理员 window并输入:

>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

路径重复了两次。好的,然后在相同的提示下我 setx 没有重复的路径,也没有 /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.

显然保存在当前用户环境中。

然后我打开一个new 非管理员命令window,和:

>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`

没变。为什么?

在 Windows 中,每个进程都会获得一份环境副本,该副本本质上是进程启动时全局环境的快照。在进程 运行 期间 对全局环境的更改不会传播回进程自己的环境副本。

为了回答实际问题,setx 确实修改了用户环境(或系统环境,如果 运行 和 /M),但更改不会立即在过程中可见执行 setx,在本例中为 cmd.exe。如果您在 运行ning setx 之后打开 new 命令提示符,您将在 that 实例中看到更改cmd.exe.

这在 setx /? 帮助中有明确说明:

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.

要在 全局环境和当前进程之一中实现相同的更改,您需要 运行 both setxset.

仅供发现此旧 post...

的任何人参考

SETX(以及大多数其他通过 PowerShell、.Net、C# 等编程的方法)仅对 运行 由 运行 设置 SETX 的用户的新进程产生直接影响。如果 SETX 是 运行 由不同的用户或系统帐户(正如大多数公司部署产品那样),任何其他用户仍需要重新启动才能接收更新。即使在受影响的用户下关闭 Explorer.exe 和 re-running 也不会刷新。

当您通过标准 Windows GUI 修改环境变量时,Microsoft 正在对 shell 环境进行一些编程魔术。