Command line to remove an environment variable from the OS level configuration

前端 未结 9 1072
闹比i
闹比i 2020-11-30 17:11

Windows has the setx command:

Description:
    Creates or modifies environment variables in the user or system
    environment.

So you can

相关标签:
9条回答
  • 2020-11-30 17:52

    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 HKCU\Environment /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 "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /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.

    0 讨论(0)
  • 2020-11-30 17:57

    The command in DougWare's answer did not work, but this did:

    reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v FOOBAR /f
    

    The shortcut HKLM can be used for HKEY_LOCAL_MACHINE.

    0 讨论(0)
  • 2020-11-30 18:02
    setx FOOBAR ""
    

    just causes the value of FOOBAR to be a null string. (Although, it shows with the set command with the "" so maybe double-quotes is the string.)

    I used:

    set FOOBAR=
    

    and then FOOBAR was no longer listed in the set command. (Log-off was not required.)

    Windows 7 32 bit, using the command prompt, non-administrator is what I used. (Not cmd or Windows + R, which might be different.)

    BTW, I did not see the variable I created anywhere in the registry after I created it. I'm using RegEdit not as administrator.

    0 讨论(0)
提交回复
热议问题