windows - 从 cmd.exe 设置持久环境变量

标签 windows batch-file environment-variables cmd

我必须在不同的 Windows 机器上设置环境变量,但我不想在“我的电脑”的属性屏幕上手动更改它们

我想使用批处理文件从命令行执行此操作。据我了解,使用 set 只会更改我将在命令窗口中调用的进程的变量。

我想明确地设置它,所以以后,当运行一个新进程时,它将使用我设置的那些新设置。有没有办法从命令行执行此操作?

最佳答案

使用 SETX command (注意“x”后缀)以设置在 cmd 窗口关闭后仍然存在的变量。

例如,设置一个值为“bar”的环境变量“foo”:

setx foo bar /m

尽管打印用法 (setx/?) 时显示的“注释”值得一读,尤其是:

  1. 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.

  2. 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/

相关文章:

php - 符号复杂的环境变量

java - 如何在 Java 中获取 JAVA_HOME 和 CATALINA_HOME 环境变量?

windows - 使 Windows 快捷方式相对于文件夹所在的位置开始?

windows - Docker 卡在 "Waiting for SSH to be available..."

windows - 使用Windows在所有目录中递归执行命令

powershell - PowerShell输出到文件

javascript - 理解 React JS 中的 .env 文件和环境变量

c# - 如何在 Windows 10 中运行在 ubuntu 中创建的 c# Visual Studio 项目

c++ - 如何将 hwnd 类型数据存储到平面文件中并将其返回到 hwnd 类型变量中?

windows - 如何创建自定义百分比 (%) 快捷方式,如 %appdata%?