我必须在不同的 Windows 机器上设置环境变量,但我不想在“我的电脑”的属性屏幕上手动更改它们
我想使用批处理文件从命令行执行此操作。据我了解,使用 set 只会更改我将在命令窗口中调用的进程的变量。
我想明确地设置它,所以以后,当运行一个新进程时,它将使用我设置的那些新设置。有没有办法从命令行执行此操作?
最佳答案
使用 SETX command (注意“x”后缀)以设置在 cmd 窗口关闭后仍然存在的变量。
例如,设置一个值为“bar”的环境变量“foo”:
setx foo bar /m
尽管打印用法 (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.
On a remote system, variables created or modified by this tool will be available at the next logon session.
在 PowerShell 中,[Environment]::SetEnvironmentVariable命令。
关于windows - 从 cmd.exe 设置持久环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5898131/