如何从cmd永久更新PATH变量?视窗

时间:2023-01-24 23:07:10

If I execute set PATH=%PATH%;C:\\Something\\bin from cmd and then execute echo %PATH% I see this string added in path. If I close and open cmd, that new string is not in PATH. How can I update PATH permanently from cmd for all processes in future, not just for the current process? I don't want't to do this by going to System Properties -> Advanced -> Environment variables and there update PATH.

如果我从cmd执行set PATH =%PATH%; C:\\ Something \\ bin然后执行echo%PATH%我看到在路径中添加了这个字符串。如果我关闭并打开cmd,则该新字符串不在PATH中。对于将来的所有进程,如何从cmd永久更新PATH,而不仅仅是针对当前进程?我不想通过转到系统属性 - >高级 - >环境变量并在那里更新PATH来做到这一点。

This command must be executed from java application (my other question).

必须从java应用程序执行此命令(我的另一个问题)。

7 个解决方案

#1


39  

The documentation on how to do this can be found on MSDN. The key extract is this:

有关如何执行此操作的文档可以在MSDN上找到。关键提取是这样的:

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.

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

Note that your application will need elevated admin rights in order to be able to modify this key.

请注意,您的应用程序需要提升管理员权限才能修改此密钥。

You indicate in the comments that you would be happy to modify just the per-user environment. Do this by editing the values in HKEY_CURRENT_USER\Environment. As before, make sure that you broadcast a WM_SETTINGCHANGE message.

您在评论中表明您很乐意仅修改每用户环境。通过编辑HKEY_CURRENT_USER \ Environment中的值来执行此操作。和以前一样,确保广播WM_SETTINGCHANGE消息。

You should be able to do this from your Java application easily enough using the JNI registry classes.

您应该能够使用JNI注册表类轻松地从Java应用程序执行此操作。

#2


141  

You can use:

您可以使用:

setx PATH "%PATH%;C:\\Something\\bin"

However, setx will truncate the stored string to 1024 bytes, potentially corrupting the PATH.

但是,setx会将存储的字符串截断为1024字节,从而可能破坏PATH。

/M will change the PATH in HKEY_LOCAL_MACHINE instead of HKEY_CURRENT_USER. In other words, a system variable, instead of the user's. For example:

/ M将更改HKEY_LOCAL_MACHINE中的PATH而不是HKEY_CURRENT_USER。换句话说,系统变量,而不是用户的。例如:

SETX /M PATH "%PATH%;C:\your path with spaces"

You have to keep in mind, the new PATH is not visible in your current cmd.exe.

您必须记住,新的PATH在您当前的cmd.exe中不可见。

But if you look in the registry or on a new cmd.exe with "set p" you can see the new value.

但是,如果您查看注册表或使用“set p”的新cmd.exe,您可以看到新值。

#3


32  

I caution against using the command

我提醒不要使用该命令

setx PATH "%PATH%;C:\Something\bin"

to modify the PATH variable because of a "feature" of its implementation. On many (most?) installations these days the variable will be lengthy - setx will truncate the stored string to 1024 bytes, potentially corrupting the PATH (see the discussion here).

修改PATH变量,因为它的实现的“功能”。在这些天的许多(大多数?)安装中,变量将是冗长的--setx会将存储的字符串截断为1024字节,可能会破坏PATH(请参阅此处的讨论)。

(I signed up specifically to flag this issue, and so lack the site reputation to directly comment on the answer posted on May 2 '12. My thanks to beresfordt for adding such a comment)

(我已经专门签署了这个问题,因此缺乏网站声誉,直接评论12月5日发布的答案。感谢beresfordt添加了这样的评论)

#4


6  

This Python-script[*] does exactly that:

这个Python脚本[*]正是这样做的:

"""
Show/Modify/Append registry env-vars (ie `PATH`) and notify Windows-applications to pickup changes.

First attempts to show/modify HKEY_LOCAL_MACHINE (all users), and 
if not accessible due to admin-rights missing, fails-back 
to HKEY_CURRENT_USER.
Write and Delete operations do not proceed to user-tree if all-users succeed.

Syntax: 
    {prog}                  : Print all env-vars. 
    {prog}  VARNAME         : Print value for VARNAME. 
    {prog}  VARNAME   VALUE : Set VALUE for VARNAME. 
    {prog}  +VARNAME  VALUE : Append VALUE in VARNAME delimeted with ';' (i.e. used for `PATH`). 
    {prog}  -VARNAME        : Delete env-var value. 

Note that the current command-window will not be affected, 
changes would apply only for new command-windows.
"""

import winreg
import os, sys, win32gui, win32con

def reg_key(tree, path, varname):
    return '%s\%s:%s' % (tree, path, varname) 

def reg_entry(tree, path, varname, value):
    return '%s=%s' % (reg_key(tree, path, varname), value)

def query_value(key, varname):
    value, type_id = winreg.QueryValueEx(key, varname)
    return value

def yield_all_entries(tree, path, key):
    i = 0
    while True:
        try:
            n,v,t = winreg.EnumValue(key, i)
            yield reg_entry(tree, path, n, v)
            i += 1
        except OSError:
            break ## Expected, this is how iteration ends.

def notify_windows(action, tree, path, varname, value):
    win32gui.SendMessage(win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0, 'Environment')
    print("---%s %s" % (action, reg_entry(tree, path, varname, value)), file=sys.stderr)

def manage_registry_env_vars(varname=None, value=None):
    reg_keys = [
        ('HKEY_LOCAL_MACHINE', r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'),
        ('HKEY_CURRENT_USER', r'Environment'),
    ]
    for (tree_name, path) in reg_keys:
        tree = eval('winreg.%s'%tree_name)
        try:
            with winreg.ConnectRegistry(None, tree) as reg:
                with winreg.OpenKey(reg, path, 0, winreg.KEY_ALL_ACCESS) as key:
                    if not varname:
                        for regent in yield_all_entries(tree_name, path, key):
                            print(regent)
                    else:
                        if not value:
                            if varname.startswith('-'):
                                varname = varname[1:]
                                value = query_value(key, varname)
                                winreg.DeleteValue(key, varname)
                                notify_windows("Deleted", tree_name, path, varname, value)
                                break  ## Don't propagate into user-tree.
                            else:
                                value = query_value(key, varname)
                                print(reg_entry(tree_name, path, varname, value))
                        else:
                            if varname.startswith('+'):
                                varname = varname[1:]
                                value = query_value(key, varname) + ';' + value
                            winreg.SetValueEx(key, varname, 0, winreg.REG_EXPAND_SZ, value)
                            notify_windows("Updated", tree_name, path, varname, value)
                            break  ## Don't propagate into user-tree.
        except PermissionError as ex:
            print("!!!Cannot access %s due to: %s" % 
                    (reg_key(tree_name, path, varname), ex), file=sys.stderr)
        except FileNotFoundError as ex:
            print("!!!Cannot find %s due to: %s" % 
                    (reg_key(tree_name, path, varname), ex), file=sys.stderr)

if __name__=='__main__':
    args = sys.argv
    argc = len(args)
    if argc > 3:
        print(__doc__.format(prog=args[0]), file=sys.stderr)
        sys.exit()

    manage_registry_env_vars(*args[1:])

Below are some usage examples, assuming it has been saved in a file called setenv.py somewhere in your current path. Note that in these examples i didn't have admin-rights, so the changes affected only my local user's registry tree:

下面是一些使用示例,假设它已保存在当前路径中某个名为setenv.py的文件中。请注意,在这些示例中,我没有管理员权限,因此更改仅影响我本地用户的注册表树:

> REM ## Print all env-vars
> setenv.py
!!!Cannot access HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session   Manager\Environment:PATH due to: [WinError 5] Access is denied
HKEY_CURRENT_USER\Environment:PATH=...
...

> REM ## Query env-var:
> setenv.py PATH C:\foo
!!!Cannot access HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session   Manager\Environment:PATH due to: [WinError 5] Access is denied
!!!Cannot find HKEY_CURRENT_USER\Environment:PATH due to: [WinError 2] The system cannot find the file specified

> REM ## Set env-var:
> setenv.py PATH C:\foo
!!!Cannot access HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session   Manager\Environment:PATH due to: [WinError 5] Access is denied
---Set HKEY_CURRENT_USER\Environment:PATH=C:\foo

> REM ## Append env-var:
> setenv.py +PATH D:\Bar
!!!Cannot access HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session   Manager\Environment:PATH due to: [WinError 5] Access is denied
---Set HKEY_CURRENT_USER\Environment:PATH=C:\foo;D:\Bar

> REM ## Delete env-var:
> setenv.py -PATH
!!!Cannot access HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session   Manager\Environment:PATH due to: [WinError 5] Access is denied
---Deleted HKEY_CURRENT_USER\Environment:PATH

[*] Adapted from: http://code.activestate.com/recipes/416087-persistent-environment-variables-on-windows/

[*]改编自:http://code.activestate.com/recipes/416087-persistent-environment-variables-on-windows/

#5


2  

For reference purpose, for anyone searching how to change the path via code, I am quoting a useful post by a Delphi programmer from this web page: http://www.tek-tips.com/viewthread.cfm?qid=686382

出于参考目的,对于任何搜索如何通过代码更改路径的人,我在这个网页上引用Delphi程序员的有用帖子:http://www.tek-tips.com/viewthread.cfm?qid = 686382

TonHu (Programmer) 22 Oct 03 17:57 I found where I read the original posting, it's here: http://news.jrsoftware.org/news/innosetup.isx/msg02129....

TonHu(程序员)08年10月22日17:57我发现我在哪里阅读原始帖子,它在这里:http://news.jrsoftware.org/news/innosetup.isx/msg02129 ....

The excerpt of what you would need is this:

您需要的摘录如下:

You must specify the string "Environment" in LParam. In Delphi you'd do it this way:

您必须在LParam中指定字符串“Environment”。在Delphi中你会这样做:

 SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, Integer(PChar('Environment')));

It was suggested by Jordan Russell, http://www.jrsoftware.org, the author of (a.o.) InnoSetup, ("Inno Setup is a free installer for Windows programs. First introduced in 1997, Inno Setup today rivals and even surpasses many commercial installers in feature set and stability.") (I just would like more people to use InnoSetup )

这是(ao)InnoSetup的作者,约翰罗素,http://www.jrsoftware.org的建议,(“Inno Setup是一个免费的Windows程序安装程序。1997年首次推出,Inno Setup今天的竞争对手甚至超过了许多功能集和稳定性的商业安装人员。“)(我希望更多人使用InnoSetup)

HTH

HTH

#6


1  

This script http://www.autohotkey.com/board/topic/63210-modify-system-path-gui/

这个脚本http://www.autohotkey.com/board/topic/63210-modify-system-path-gui/

includes all the necessary Windows API calls which can be refactored for your needs. It is actually an AutoHotkey GUI to change the System PATH easily. Needs to be run as an Administrator.

包括所有必要的Windows API调用,可以根据您的需要进行重构。它实际上是一个AutoHotkey GUI,可以轻松更改系统路径。需要以管理员身份运行。

#7


1  

In a corporate network, where the user has only limited access and uses portable apps, there are these command line tricks:

在公司网络中,用户只能访问受限并使用便携式应用程序,这些命令行技巧有:

  1. Query the user env variables: reg query "HKEY_CURRENT_USER\Environment". Use "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" for LOCAL_MACHINE.
  2. 查询用户env变量:reg query“HKEY_CURRENT_USER \ Environment”。对LOCAL_MACHINE使用“HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Environment”。
  3. Add new user env variable: reg add "HKEY_CURRENT_USER\Environment" /v shared_dir /d "c:\shared" /t REG_SZ. Use REG_EXPAND_SZ for paths containing other %% variables.
  4. 添加新用户env变量:reg add“HKEY_CURRENT_USER \ Environment”/ v shared_dir / d“c:\ shared”/ t REG_SZ。将REG_EXPAND_SZ用于包含其他%%变量的路径。
  5. Delete existing env variable: reg delete "HKEY_CURRENT_USER\Environment" /v shared_dir.
  6. 删除现有的env变量:reg delete“HKEY_CURRENT_USER \ Environment”/ v shared_dir。

#1


39  

The documentation on how to do this can be found on MSDN. The key extract is this:

有关如何执行此操作的文档可以在MSDN上找到。关键提取是这样的:

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.

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

Note that your application will need elevated admin rights in order to be able to modify this key.

请注意,您的应用程序需要提升管理员权限才能修改此密钥。

You indicate in the comments that you would be happy to modify just the per-user environment. Do this by editing the values in HKEY_CURRENT_USER\Environment. As before, make sure that you broadcast a WM_SETTINGCHANGE message.

您在评论中表明您很乐意仅修改每用户环境。通过编辑HKEY_CURRENT_USER \ Environment中的值来执行此操作。和以前一样,确保广播WM_SETTINGCHANGE消息。

You should be able to do this from your Java application easily enough using the JNI registry classes.

您应该能够使用JNI注册表类轻松地从Java应用程序执行此操作。

#2


141  

You can use:

您可以使用:

setx PATH "%PATH%;C:\\Something\\bin"

However, setx will truncate the stored string to 1024 bytes, potentially corrupting the PATH.

但是,setx会将存储的字符串截断为1024字节,从而可能破坏PATH。

/M will change the PATH in HKEY_LOCAL_MACHINE instead of HKEY_CURRENT_USER. In other words, a system variable, instead of the user's. For example:

/ M将更改HKEY_LOCAL_MACHINE中的PATH而不是HKEY_CURRENT_USER。换句话说,系统变量,而不是用户的。例如:

SETX /M PATH "%PATH%;C:\your path with spaces"

You have to keep in mind, the new PATH is not visible in your current cmd.exe.

您必须记住,新的PATH在您当前的cmd.exe中不可见。

But if you look in the registry or on a new cmd.exe with "set p" you can see the new value.

但是,如果您查看注册表或使用“set p”的新cmd.exe,您可以看到新值。

#3


32  

I caution against using the command

我提醒不要使用该命令

setx PATH "%PATH%;C:\Something\bin"

to modify the PATH variable because of a "feature" of its implementation. On many (most?) installations these days the variable will be lengthy - setx will truncate the stored string to 1024 bytes, potentially corrupting the PATH (see the discussion here).

修改PATH变量,因为它的实现的“功能”。在这些天的许多(大多数?)安装中,变量将是冗长的--setx会将存储的字符串截断为1024字节,可能会破坏PATH(请参阅此处的讨论)。

(I signed up specifically to flag this issue, and so lack the site reputation to directly comment on the answer posted on May 2 '12. My thanks to beresfordt for adding such a comment)

(我已经专门签署了这个问题,因此缺乏网站声誉,直接评论12月5日发布的答案。感谢beresfordt添加了这样的评论)

#4


6  

This Python-script[*] does exactly that:

这个Python脚本[*]正是这样做的:

"""
Show/Modify/Append registry env-vars (ie `PATH`) and notify Windows-applications to pickup changes.

First attempts to show/modify HKEY_LOCAL_MACHINE (all users), and 
if not accessible due to admin-rights missing, fails-back 
to HKEY_CURRENT_USER.
Write and Delete operations do not proceed to user-tree if all-users succeed.

Syntax: 
    {prog}                  : Print all env-vars. 
    {prog}  VARNAME         : Print value for VARNAME. 
    {prog}  VARNAME   VALUE : Set VALUE for VARNAME. 
    {prog}  +VARNAME  VALUE : Append VALUE in VARNAME delimeted with ';' (i.e. used for `PATH`). 
    {prog}  -VARNAME        : Delete env-var value. 

Note that the current command-window will not be affected, 
changes would apply only for new command-windows.
"""

import winreg
import os, sys, win32gui, win32con

def reg_key(tree, path, varname):
    return '%s\%s:%s' % (tree, path, varname) 

def reg_entry(tree, path, varname, value):
    return '%s=%s' % (reg_key(tree, path, varname), value)

def query_value(key, varname):
    value, type_id = winreg.QueryValueEx(key, varname)
    return value

def yield_all_entries(tree, path, key):
    i = 0
    while True:
        try:
            n,v,t = winreg.EnumValue(key, i)
            yield reg_entry(tree, path, n, v)
            i += 1
        except OSError:
            break ## Expected, this is how iteration ends.

def notify_windows(action, tree, path, varname, value):
    win32gui.SendMessage(win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0, 'Environment')
    print("---%s %s" % (action, reg_entry(tree, path, varname, value)), file=sys.stderr)

def manage_registry_env_vars(varname=None, value=None):
    reg_keys = [
        ('HKEY_LOCAL_MACHINE', r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'),
        ('HKEY_CURRENT_USER', r'Environment'),
    ]
    for (tree_name, path) in reg_keys:
        tree = eval('winreg.%s'%tree_name)
        try:
            with winreg.ConnectRegistry(None, tree) as reg:
                with winreg.OpenKey(reg, path, 0, winreg.KEY_ALL_ACCESS) as key:
                    if not varname:
                        for regent in yield_all_entries(tree_name, path, key):
                            print(regent)
                    else:
                        if not value:
                            if varname.startswith('-'):
                                varname = varname[1:]
                                value = query_value(key, varname)
                                winreg.DeleteValue(key, varname)
                                notify_windows("Deleted", tree_name, path, varname, value)
                                break  ## Don't propagate into user-tree.
                            else:
                                value = query_value(key, varname)
                                print(reg_entry(tree_name, path, varname, value))
                        else:
                            if varname.startswith('+'):
                                varname = varname[1:]
                                value = query_value(key, varname) + ';' + value
                            winreg.SetValueEx(key, varname, 0, winreg.REG_EXPAND_SZ, value)
                            notify_windows("Updated", tree_name, path, varname, value)
                            break  ## Don't propagate into user-tree.
        except PermissionError as ex:
            print("!!!Cannot access %s due to: %s" % 
                    (reg_key(tree_name, path, varname), ex), file=sys.stderr)
        except FileNotFoundError as ex:
            print("!!!Cannot find %s due to: %s" % 
                    (reg_key(tree_name, path, varname), ex), file=sys.stderr)

if __name__=='__main__':
    args = sys.argv
    argc = len(args)
    if argc > 3:
        print(__doc__.format(prog=args[0]), file=sys.stderr)
        sys.exit()

    manage_registry_env_vars(*args[1:])

Below are some usage examples, assuming it has been saved in a file called setenv.py somewhere in your current path. Note that in these examples i didn't have admin-rights, so the changes affected only my local user's registry tree:

下面是一些使用示例,假设它已保存在当前路径中某个名为setenv.py的文件中。请注意,在这些示例中,我没有管理员权限,因此更改仅影响我本地用户的注册表树:

> REM ## Print all env-vars
> setenv.py
!!!Cannot access HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session   Manager\Environment:PATH due to: [WinError 5] Access is denied
HKEY_CURRENT_USER\Environment:PATH=...
...

> REM ## Query env-var:
> setenv.py PATH C:\foo
!!!Cannot access HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session   Manager\Environment:PATH due to: [WinError 5] Access is denied
!!!Cannot find HKEY_CURRENT_USER\Environment:PATH due to: [WinError 2] The system cannot find the file specified

> REM ## Set env-var:
> setenv.py PATH C:\foo
!!!Cannot access HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session   Manager\Environment:PATH due to: [WinError 5] Access is denied
---Set HKEY_CURRENT_USER\Environment:PATH=C:\foo

> REM ## Append env-var:
> setenv.py +PATH D:\Bar
!!!Cannot access HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session   Manager\Environment:PATH due to: [WinError 5] Access is denied
---Set HKEY_CURRENT_USER\Environment:PATH=C:\foo;D:\Bar

> REM ## Delete env-var:
> setenv.py -PATH
!!!Cannot access HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session   Manager\Environment:PATH due to: [WinError 5] Access is denied
---Deleted HKEY_CURRENT_USER\Environment:PATH

[*] Adapted from: http://code.activestate.com/recipes/416087-persistent-environment-variables-on-windows/

[*]改编自:http://code.activestate.com/recipes/416087-persistent-environment-variables-on-windows/

#5


2  

For reference purpose, for anyone searching how to change the path via code, I am quoting a useful post by a Delphi programmer from this web page: http://www.tek-tips.com/viewthread.cfm?qid=686382

出于参考目的,对于任何搜索如何通过代码更改路径的人,我在这个网页上引用Delphi程序员的有用帖子:http://www.tek-tips.com/viewthread.cfm?qid = 686382

TonHu (Programmer) 22 Oct 03 17:57 I found where I read the original posting, it's here: http://news.jrsoftware.org/news/innosetup.isx/msg02129....

TonHu(程序员)08年10月22日17:57我发现我在哪里阅读原始帖子,它在这里:http://news.jrsoftware.org/news/innosetup.isx/msg02129 ....

The excerpt of what you would need is this:

您需要的摘录如下:

You must specify the string "Environment" in LParam. In Delphi you'd do it this way:

您必须在LParam中指定字符串“Environment”。在Delphi中你会这样做:

 SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, Integer(PChar('Environment')));

It was suggested by Jordan Russell, http://www.jrsoftware.org, the author of (a.o.) InnoSetup, ("Inno Setup is a free installer for Windows programs. First introduced in 1997, Inno Setup today rivals and even surpasses many commercial installers in feature set and stability.") (I just would like more people to use InnoSetup )

这是(ao)InnoSetup的作者,约翰罗素,http://www.jrsoftware.org的建议,(“Inno Setup是一个免费的Windows程序安装程序。1997年首次推出,Inno Setup今天的竞争对手甚至超过了许多功能集和稳定性的商业安装人员。“)(我希望更多人使用InnoSetup)

HTH

HTH

#6


1  

This script http://www.autohotkey.com/board/topic/63210-modify-system-path-gui/

这个脚本http://www.autohotkey.com/board/topic/63210-modify-system-path-gui/

includes all the necessary Windows API calls which can be refactored for your needs. It is actually an AutoHotkey GUI to change the System PATH easily. Needs to be run as an Administrator.

包括所有必要的Windows API调用,可以根据您的需要进行重构。它实际上是一个AutoHotkey GUI,可以轻松更改系统路径。需要以管理员身份运行。

#7


1  

In a corporate network, where the user has only limited access and uses portable apps, there are these command line tricks:

在公司网络中,用户只能访问受限并使用便携式应用程序,这些命令行技巧有:

  1. Query the user env variables: reg query "HKEY_CURRENT_USER\Environment". Use "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" for LOCAL_MACHINE.
  2. 查询用户env变量:reg query“HKEY_CURRENT_USER \ Environment”。对LOCAL_MACHINE使用“HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Environment”。
  3. Add new user env variable: reg add "HKEY_CURRENT_USER\Environment" /v shared_dir /d "c:\shared" /t REG_SZ. Use REG_EXPAND_SZ for paths containing other %% variables.
  4. 添加新用户env变量:reg add“HKEY_CURRENT_USER \ Environment”/ v shared_dir / d“c:\ shared”/ t REG_SZ。将REG_EXPAND_SZ用于包含其他%%变量的路径。
  5. Delete existing env variable: reg delete "HKEY_CURRENT_USER\Environment" /v shared_dir.
  6. 删除现有的env变量:reg delete“HKEY_CURRENT_USER \ Environment”/ v shared_dir。