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

发布于 2024-11-05 09:39:09 字数 180 浏览 1 评论 0 原文

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

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

我想明确地设置它,所以稍后,当运行新进程时,它将使用我设置的那些新设置。有没有办法从命令行做到这一点?

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"

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

假扮的天使 2024-11-12 09:39:09

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

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

setx foo bar /m

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

  • 在本地系统上,通过此工具创建或修改的变量将在未来的命令窗口中可用,但在当前的 CMD.exe 命令窗口中不可用

  • 在远程系统上,通过此工具创建或修改的变量将在下一次登录会话中可用。

  • 在 PowerShell 中,[Environment]::SetEnvironmentVariable 命令。

    Use the SETX command (note the 'x' suffix) to set variables that persist after the cmd window has been closed.

    For example, to set an env var "foo" with value of "bar":

    setx foo bar /m
    

    Though it's worth reading the 'notes' that are displayed if you print the usage (setx /?), in particular:

    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.

    In PowerShell, the [Environment]::SetEnvironmentVariable command.

    偏闹i 2024-11-12 09:39:09

    环境变量的 MSDN 文档告诉您该怎么做:

    要以编程方式添加或修改系统环境变量,请将它们添加到 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment 注册表项,然后广播 WM_SETTINGCHANGE 消息lParam 设置为字符串“Environment”。这允许应用程序(例如 shell)获取您的更新。

    您当然需要管理员权限才能执行此操作。我知道没有办法从 Windows 批处理中广播 Windows 消息,因此您需要编写一个小程序来执行此操作。

    The MSDN documentation for environment variables tells you what to do:

    To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment". This allows applications, such as the shell, to pick up your updates.

    You will of course need admin rights to do this. I know of no way to broadcast a windows message from Windows batch so you'll need to write a small program to do this.

    愁杀 2024-11-12 09:39:09
    :: Sets environment variables for both the current `cmd` window 
    ::   and/or other applications going forward.
    :: I call this file keyz.cmd to be able to just type `keyz` at the prompt 
    ::   after changes because the word `keys` is already taken in Windows.
    
    @echo off
    
    :: set for the current window
    set APCA_API_KEY_ID=key_id
    set APCA_API_SECRET_KEY=secret_key
    set APCA_API_BASE_URL=https://paper-api.alpaca.markets
    
    :: setx also for other windows and processes going forward
    setx APCA_API_KEY_ID     %APCA_API_KEY_ID%
    setx APCA_API_SECRET_KEY %APCA_API_SECRET_KEY%
    setx APCA_API_BASE_URL   %APCA_API_BASE_URL%
    
    :: Displaying what was just set.
    set apca
    
    :: Or for copy/paste manually ...
    :: setx APCA_API_KEY_ID     'key_id'
    :: setx APCA_API_SECRET_KEY 'secret_key'
    :: setx APCA_API_BASE_URL   'https://paper-api.alpaca.markets'
    
    :: Sets environment variables for both the current `cmd` window 
    ::   and/or other applications going forward.
    :: I call this file keyz.cmd to be able to just type `keyz` at the prompt 
    ::   after changes because the word `keys` is already taken in Windows.
    
    @echo off
    
    :: set for the current window
    set APCA_API_KEY_ID=key_id
    set APCA_API_SECRET_KEY=secret_key
    set APCA_API_BASE_URL=https://paper-api.alpaca.markets
    
    :: setx also for other windows and processes going forward
    setx APCA_API_KEY_ID     %APCA_API_KEY_ID%
    setx APCA_API_SECRET_KEY %APCA_API_SECRET_KEY%
    setx APCA_API_BASE_URL   %APCA_API_BASE_URL%
    
    :: Displaying what was just set.
    set apca
    
    :: Or for copy/paste manually ...
    :: setx APCA_API_KEY_ID     'key_id'
    :: setx APCA_API_SECRET_KEY 'secret_key'
    :: setx APCA_API_BASE_URL   'https://paper-api.alpaca.markets'
    
    生来就爱笑 2024-11-12 09:39:09

    事实上 SET TEST_VARIABLE=value 仅适用于当前进程,因此需要 SETX 。在用户级别永久存储环境变量的快速示例。

    1. 在cmd中,SETX TEST_VARIABLE等尚未应用(echo %TEST_VARIABLE%显示%TEST_VARIABLE%
    2. 快速检查:打开cmd,echo %TEST_VARIABLE%显示etc
    3. :系统属性->环境变量->用户变量 -> 您应该看到带有值 etc 的变量 TEST_VARIABLE。

    Indeed SET TEST_VARIABLE=value works for current process only, so SETX is required. A quick example for permanently storing an environment variable at user level.

    1. In cmd, SETX TEST_VARIABLE etc. Not applied yet (echo %TEST_VARIABLE% shows %TEST_VARIABLE%,
    2. Quick check: open cmd, echo %TEST_VARIABLE% shows etc.
    3. GUI check: System Properties -> Advanced -> Environment variables -> User variables for -> you should see Varible TEST_VARIABLE with value etc.
    玩物 2024-11-12 09:39:09

    VBScript (.vbs) 的示例

    Sub sety(wsh, action, typey, vary, value)
      Dim wu
      Set wu = wsh.Environment(typey)
      wui = wu.Item(vary)
      Select Case action
        Case "ls"
          WScript.Echo wui
        Case "del"
          On Error Resume Next
          wu.remove(vary)
          On Error Goto 0
        Case "set"
          wu.Item(vary) = value
        Case "add"
          If wui = "" Then
            wu.Item(vary) = value
          ElseIf InStr(UCase(";" & wui & ";"), UCase(";" & value & ";")) = 0 Then
            wu.Item(vary) = value & ";" & wui
          End If
        Case Else
          WScript.Echo "Bad action"
      End Select
    End Sub
    
    Dim wsh, args
    Set wsh = WScript.CreateObject("WScript.Shell")
    Set args = WScript.Arguments
    Select Case WScript.Arguments.Length
      Case 3
        value = ""
      Case 4
        value = args(3)
      Case Else
        WScript.Echo "Arguments - 0: ls,del,set,add; 1: user,system, 2: variable; 3: value"
        value = "```"
    End Select
    If Not value = "```" Then
      ' 0: ls,del,set,add; 1: user,system, 2: variable; 3: value
      sety wsh, args(0), args(1), UCase(args(2)), value
    End If
    

    An example with VBScript (.vbs)

    Sub sety(wsh, action, typey, vary, value)
      Dim wu
      Set wu = wsh.Environment(typey)
      wui = wu.Item(vary)
      Select Case action
        Case "ls"
          WScript.Echo wui
        Case "del"
          On Error Resume Next
          wu.remove(vary)
          On Error Goto 0
        Case "set"
          wu.Item(vary) = value
        Case "add"
          If wui = "" Then
            wu.Item(vary) = value
          ElseIf InStr(UCase(";" & wui & ";"), UCase(";" & value & ";")) = 0 Then
            wu.Item(vary) = value & ";" & wui
          End If
        Case Else
          WScript.Echo "Bad action"
      End Select
    End Sub
    
    Dim wsh, args
    Set wsh = WScript.CreateObject("WScript.Shell")
    Set args = WScript.Arguments
    Select Case WScript.Arguments.Length
      Case 3
        value = ""
      Case 4
        value = args(3)
      Case Else
        WScript.Echo "Arguments - 0: ls,del,set,add; 1: user,system, 2: variable; 3: value"
        value = "```"
    End Select
    If Not value = "```" Then
      ' 0: ls,del,set,add; 1: user,system, 2: variable; 3: value
      sety wsh, args(0), args(1), UCase(args(2)), value
    End If
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文