从操作系统级别配置中删除环境变量的命令行 [英] Command line to remove an environment variable from the OS level configuration
问题描述
Windows 有
那么你将如何实际删除变量?
要从当前环境中删除变量(不是永久):
设置 FOOBAR=
从 user 环境中永久删除变量(这是 setx
放置它的默认位置):
REG 删除 HKCUEnvironment/F/V FOOBAR
如果变量是在 system 环境中设置的(例如,如果您最初使用 setx/M
设置它),以管理员身份运行:
REG 删除 "HKLMSYSTEMCurrentControlSetControlSession ManagerEnvironment"/F/V FOOBAR
注意:上面的 REG
命令不会影响任何现有进程(以及一些从现有进程派生的新进程),因此如果更改立即生效很重要,最简单和最可靠的做法是注销并重新登录或重新启动.如果这不是一个选项,或者您想更深入地挖掘,这里的一些其他答案有一些可能适合您的用例的好建议.
Windows has the setx
command:
Description:
Creates or modifies environment variables in the user or system
environment.
So you can set a variable like this:
setx FOOBAR 1
And you can clear the value like this:
setx FOOBAR ""
However, the variable does not get removed. It stays in the registry:
So how would you actually remove the variable?
To remove the variable from the current environment (not permanently):
set FOOBAR=
To permanently remove the variable from the user environment (which is the default place setx
puts it):
REG delete HKCUEnvironment /F /V FOOBAR
If the variable is set in the system environment (e.g. if you originally set it with setx /M
), as an administrator run:
REG delete "HKLMSYSTEMCurrentControlSetControlSession ManagerEnvironment" /F /V FOOBAR
Note: The REG
commands above won't affect any existing processes (and some new processes that are forked from existing processes), so if it's important for the change to take effect immediately, the easiest and surest thing to do is log out and back in or reboot. If this isn't an option or you want to dig deeper, some of the other answers here have some great suggestions that may suit your use case.
这篇关于从操作系统级别配置中删除环境变量的命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!