如何使用批处理文件从系统配置中删除环境变量 [英] How to remove an environment variable from the system configuration with a batch file

查看:1024
本文介绍了如何使用批处理文件从系统配置中删除环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从客户端工作站中删除系统变量。我有500多个客户端,所以我想提供批处理文件给用户自己去删除系统变量。

解决方案

 您可能希望将这两个永久性设置为setx,但显然无需

C:\> set uvar = HKCU\Environment
C: \>

C:\> set mvar = HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
C:\>

一些术语

一个键就像一个文件夹),你有一个变量,一些数据像a = 5



http://msdn.microsoft.com/en-us/library/3dwk5axy.aspx
谈论名称(a)具有值(5)。



注册表术语在msdn上看起来有点不同,但reg.exe和维基百科似乎一致。
维基百科 http://en.wikipedia.org/wiki/Windows_Registry 说它
注册表值是存储在密钥中的名称/数据对
谈论名称/数据对。名称(a)数据(5),而一对是一个值。



REG似乎使用/ v,它可能是指变量名称及其数据(如维基百科),它显示变量名称及其数据。而REG有一个/ d,我认为只是数据。



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查询%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

如果您尝试使用setx删除它,最接近的是将其清除为该MS KB文章这个MS知识库文章195050 说,留下你它仍然在注册表中。



setx不从注册表中删除变量

  C:\> setx aaa

成功:指定的值已保存。

C:\> reg查询%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:\>

值得注意的是,该变量已经到达cmd提示符。 echo%aaa%将显示%aaa%,因此该变量不见了。现在注册表中只有一些不必要的垃圾。



用户提出了一个有趣的一点,现在当您尝试设置一个系统范围的变量时,它不起作用。因为它被用户变量中的内容覆盖,在这种情况下,它将始终是未定义的。

  C: \> reg delete%uvar%/ v aaa 
删除注册表值aaa(是/否)? y
操作成功完成。

C:\>

请注意,aaa现在已经不在了。

  C:\> reg查询%uvar%

HKEY_CURRENT_USER\Environment
TEMP REG_EXPAND_SZ%USERPROFILE%\AppData\Local\Temp
TMP REG_EXPAND_SZ%USERPROFILE%\AppData\Local\Temp
...


C:\>

如果您想为系统/机器环境变量而不是用户环境变量执行此操作。 p>

- 以下行必须来自管理特权cmd提示符

-notice的关键是HKLM\SYSTEM\CurrentControlSet\Control\Session经理\环境

- %mvar%(不像%uvar%)需要引号,它的键有一个空格。如果你错过了这个引号,那就没有什么大不了的了,你只是得到一个可能提醒你使用引号的错误。

- 当然如果你想在setx的注册表部分设置一个环境变量,使用例如setx aaa 5 -m ie -m after。

  C:\> set mvar = HKLM\SYSTEM\ CurrentControlSet\Control\Session Manager\Environment 

C:\> reg delete%mvar%/ v aaa
删除注册表值aaa(是/否)? y
操作成功完成。

C:\>

进一步说明

您可以使用REG ADD或REG DELETE ..(代替setx)创建一个环境变量,但是无论哪种方式,设置变量时,您可能也想使用set,因此它也为当前会话设置,而不仅仅是下一个。链接重设 https://superuser.com/questions/647505/set-enviroment-variable-setx


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.

解决方案

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

http://msdn.microsoft.com/en-us/library/3dwk5axy.aspx talks about name(a) having a value(5).

Registry terminology looks a bit different on msdn but reg.exe and wikipedia seem consistent. wikipedia http://en.wikipedia.org/wiki/Windows_Registry says 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.

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.

An example with 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 not removing the variable from the registry

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.

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.

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.

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

这篇关于如何使用批处理文件从系统配置中删除环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆