windows 如何使用批处理文件从系统配置中删除环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1472722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove an environment variable from the system configuration with a batch file
提问by user73628
I need to remove system variables from client workstations. I have 500+ clients, so I want to provide batch file to user to run himself to delete the system variables.
我需要从客户端工作站中删除系统变量。我有 500 多个客户端,所以我想提供批处理文件给用户自己运行以删除系统变量。
回答by barlop
You may want to make these two permanent with setx but obviously no need to
C:\>set uvar=HKCU\Environment
C:\>
C:\>set mvar=HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
C:\>
Some terminology
Within a Key(which is like a folder), you have a variable with some data like a=5
一些术语
在一个键(就像一个文件夹)中,你有一个变量,其中包含一些数据,例如 a=5
http://msdn.microsoft.com/en-us/library/3dwk5axy.aspxtalks about name(a) having a value(5).
http://msdn.microsoft.com/en-us/library/3dwk5axy.aspx谈论名称(a)具有值(5)。
Registry terminology looks a bit different on msdn but reg.exe and wikipedia seem consistent. wikipedia http://en.wikipedia.org/wiki/Windows_Registrysays so it "Registry values are name/data pairs stored within keys" talks of a name/data pair. name(a) data(5) and the pair together being a value.
msdn 上的注册表术语看起来有点不同,但 reg.exe 和 wikipedia 似乎是一致的。维基百科http://en.wikipedia.org/wiki/Windows_Registry这样说它“注册表值是存储在键中的名称/数据对”谈论名称/数据对。name(a) data(5) 和这对一起是一个值。
REG seems to use /v which probably refers to both variable name and its data (as in wikipedia), it does show both the variable name and its data. And REG has a /d which I suppose is for just data.
REG 似乎使用 /v 可能是指变量名及其数据(如在维基百科中),它确实显示了变量名及其数据。并且 REG 有一个 /d,我认为它仅用于数据。
An example with setx
setx 的示例
C:\>reg query %uvar%
HKEY_CURRENT_USER\Environment
TEMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
TMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
...
C:\>setx aaa 3
C:\>reg query %uvar%
HKEY_CURRENT_USER\Environment
TEMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
TMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
...
aaa REG_SZ 3
If you try to use setx to remove it, the closest you can get is clearing it as this MS KB article this MS KB article 195050 says which leaves you with it still in the registry.
如果您尝试使用 setx 来删除它,那么您可以获得的最接近的方法是清除它,因为这篇 MS KB 文章 这篇 MS KB 文章 195050说这让您仍然在注册表中。
setx not removing the variable from the registry
setx 没有从注册表中删除变量
C:\>setx aaa ""
SUCCESS: Specified value was saved.
C:\>reg query %uvar%
HKEY_CURRENT_USER\Environment
TEMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
TMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
...
aaa REG_SZ
C:\>
It's worth noting though, that the variable is gone as far as the cmd prompt is concerned. echo %aaa% will display %aaa% so the variable is gone. There's just some unnecessary junk in the registry now.
不过值得注意的是,就 cmd 提示符而言,该变量已经消失了。echo %aaa% 将显示 %aaa% 所以变量消失了。现在注册表中只有一些不必要的垃圾。
user makes an interesting point, that now when you try to set a system wide variable, it doesn't work. as it gets overwritten by what is in the user variable, in this case, it will always come up as undefined.
用户提出了一个有趣的观点,现在当您尝试设置系统范围的变量时,它不起作用。当它被用户变量中的内容覆盖时,在这种情况下,它将始终显示为未定义。
C:\>reg delete %uvar% /v aaa
Delete the registry value aaa (Yes/No)? y
The operation completed successfully.
C:\>
Notice that aaa is now gone. Below.
请注意, aaa 现在消失了。以下。
C:\>reg query %uvar%
HKEY_CURRENT_USER\Environment
TEMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
TMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
...
C:\>
If you want to do it for a system/machine environment variable rather than a user one.
如果您想为系统/机器环境变量而不是用户环境变量执行此操作。
-the lines below must be from an administrative privileged cmd prompt
-notice the key is HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
-The %mvar% (unlike %uvar%) needs quotes round it 'cos the key has a space in it. If you miss the quotes it's no big deal you just get an error which might remind you to use quotes.
-And of course if you want to set an environment variable in that part of the registry with setx, use e.g. setx aaa 5 -m i.e. the -m after.
-下面的行必须来自管理特权的 cmd 提示符 -
注意密钥是 HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
-%mvar%(与 %uvar% 不同)需要引号,因为密钥有其中的空间。如果您错过了引号,那没什么大不了的,您只会收到一个错误,提醒您使用引号。
-当然,如果您想使用 setx 在注册表的那部分设置环境变量,请使用例如 setx aaa 5 -m 即 -m 之后。
C:\>set mvar=HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
C:\>reg delete "%mvar%" /v aaa
Delete the registry value aaa (Yes/No)? y
The operation completed successfully.
C:\>
further note
You might be able to create an environment variable just with REG ADD or REG DELETE..(in place of setx) but either way, when setting a variable you may want to use set too, so that it is set for the current session too not just the next one. And a link re setx https://superuser.com/questions/647505/set-enviroment-variable-setx
进一步说明
您可以仅使用 REG ADD 或 REG DELETE ..(代替 setx)创建环境变量,但无论哪种方式,在设置变量时,您可能也想使用 set,以便为当前设置会议也不仅仅是下一个。和一个链接重新 setx https://superuser.com/questions/647505/set-enviroment-variable-setx
回答by user1921254
The setx command with an empty string value doesn't delete it. And that is not just nitpicking, because the logic is quite different. Because if the user specific variable overrides a global variable, then it is not possible to remove the user specific variable and have the global variable "become active" again. The empty string user specific variable blocks the global variable. One need to actually DELETEit (using the Evironment Variables window) to achieve that.
带有空字符串值的 setx 命令不会删除它。这不仅仅是吹毛求疵,因为逻辑完全不同。因为如果用户特定变量覆盖全局变量,则不可能删除用户特定变量并使全局变量再次“变为活动状态”。空字符串用户特定变量阻止全局变量。需要实际删除它(使用环境变量窗口)来实现这一点。
I just today tried this, with the global variable JAVA_HOME being overridden by a user specific JAVA_HOME. It was not possible to make the global varialble active again without deleting the user specific variable completely. And so far I have not seen a method to do that from the command line or batch script.
我今天刚刚尝试过这个,全局变量 JAVA_HOME 被用户特定的 JAVA_HOME 覆盖。在不完全删除用户特定变量的情况下,不可能再次激活全局变量。到目前为止,我还没有看到从命令行或批处理脚本执行此操作的方法。
(Not enough reputation points to add comment to the above answer about setx, so have to add my own answer...)
(没有足够的声誉点来为上述关于 setx 的答案添加评论,所以必须添加我自己的答案......)
回答by Eric J.
You will need to modify this registry key:
您将需要修改此注册表项:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
Changes are effective after rebooting.
更改在重新启动后生效。
回答by Dan P.
If you want to avoid the reboot, you will to find a way to broadcast a WM_SETTINGSCHANGE
to every active window after you have deleted the value.
如果您想避免重新启动,您将找到一种方法,WM_SETTINGSCHANGE
在删除该值后向每个活动窗口广播 a 。
That will make new CMD windows notice that the variable has been deleted (and any other program that understands WM_SETTNGSCHANGE
).
这将使新的 CMD 窗口注意到该变量已被删除(以及任何其他理解 的程序WM_SETTNGSCHANGE
)。