问题描述
我必须在不同的 Windows 机器上设置环境变量,但我不想通过进入我的电脑"的属性屏幕来手动更改它们
I have to set environment variables on different windows machines, but I don't want to be bothered changing them manually by getting on the properties screen of "My Computer"
我想使用批处理文件从命令行执行此操作.据我了解,使用 set 只会更改我将在命令窗口中调用的进程的变量.
I want to do it from the command line, with a batch file. As far as I understand, using set will only change the variable for the processes I will call in the command window.
我想确定设置,所以以后运行新进程时,它会使用我设置的那些新设置.有没有办法从命令行做到这一点?
I want to set it definitely, so later, when running a new process, it will use those new settings I have set. Is there a way to do that from the command line ?
推荐答案
使用 SETX 命令(注意x"后缀)来设置在 cmd 窗口关闭后仍然存在的变量.
Use the SETX command (note the 'x' suffix) to set variables that persist after the cmd window has been closed.
例如,设置一个值为bar"的环境变量foo":
For example, to set an env var "foo" with value of "bar":
setx foo bar
虽然值得阅读如果您打印用法(setx/?
)时显示的注释",特别是:
Though it's worth reading the 'notes' that are displayed if you print the usage (setx /?
), in particular:
2) 在本地系统上,此工具创建或修改的变量将在未来的命令窗口中可用,但在当前的 CMD.exe 命令窗口中不可用.
3) 在远程系统上,此工具创建或修改的变量将在下一次登录会话中可用.
3) On a remote system, variables created or modified by this tool will be available at the next logon session.
在 PowerShell 中,[Environment]::SetEnvironmentVariable 命令.
In PowerShell, the [Environment]::SetEnvironmentVariable command.
这篇关于从 cmd.exe 设置持久环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!