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

前端 未结 9 1070
闹比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:41

    From PowerShell you can use the .NET [System.Environment]::SetEnvironmentVariable() method:

    • To remove a user environment variable named FOO:

      [Environment]::SetEnvironmentVariable('FOO', $null, 'User')
      

    Note that $null is used to better signal the intent to remove the variable, though technically it is effectively the same as passing '' in this case.

    • To remove a system (machine-level) environment variable named FOO - requires elevation (must be run as administrator):

      [Environment]::SetEnvironmentVariable('FOO', $null, 'Machine')
      

    Aside from faster execution, the advantage over the reg.exe-based method is that other applications are notified of the change, via a WM_SETTINGCHANGE message (though not all applications listen to that message).

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

    You can also create a small VBScript script:

    Set env = CreateObject("WScript.Shell").Environment("System")
    If env(WScript.Arguments(0)) <> vbNullString Then env.Remove WScript.Arguments(0)
    

    Then call it like %windir%\System32\cscript.exe //Nologo "script_name.vbs" FOOBAR.

    The disadvantage is you need an extra script, but it does not require a reboot.

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

    I agree with CupawnTae.

    SET is not useful for changes to the master environment.

    FYI: System variables are in HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment (a good deal longer than user vars).

    The full command for a system var named FOOBAR therefore is:

    REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V FOOBAR
    

    (Note the quotes required to handle the space.)

    It is too bad the setx command doesn't support a delete syntax. :(

    PS: Use responsibly - If you kill your path variable, don't blame me!

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

    Delete Without Rebooting

    The OP's question indeed has been answered extensively, including how to avoid rebooting through powershell, vbscript, or you name it.

    However, if you need to stick to cmd commands only and don't have the luxury of being able to call powershell or vbscript, you could use the following approach:

    rem remove from current cmd instance
      SET FOOBAR=
    rem remove from the registry if it's a user variable
      REG delete HKCU\Environment /F /V FOOBAR
    rem remove from the registry if it's a system variable
      REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V FOOBAR
    rem tell Explorer.exe to reload the environment from the registry
      SETX DUMMY ""
    rem remove the dummy
      REG delete HKCU\Environment /F /V DUMMY
    

    So the magic here is that by using "setx" to assign something to a variable you don't need (in my example DUMMY), you force Explorer.exe to reread the variables from the registry, without needing powershell. You then clean up that dummy, and even though that one will stay in Explorer's environment for a little while longer, it will probably not harm anyone.

    Or if after deleting variables you need to set new ones, then you don't even need any dummy. Just using SETX to set the new variables will automatically clear the ones you just removed from any new cmd tasks that might get started.

    Background information: I just used this approach successfully to replace a set of user variables by system variables of the same name on all of the computers at my job, by modifying an existing cmd script. There are too many computers to do it manually, nor was it practical to copy extra powershell or vbscripts to all of them. The reason I urgently needed to replace user with system variables was that user variables get synchronized in roaming profiles (didn't think about that), so multiple machines using the same windows login but needing different values, got mixed up.

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

    To remove the variable from the current command session without removing it permanently, use the regular built-in set command - just put nothing after the equals sign:

    set FOOBAR=

    To confirm, run set with no arguments and check the current environment. The variable should be missing from the list entirely.

    Note: this will only remove the variable from the current environment - it will not persist the change to the registry. When a new command process is started, the variable will be back.

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

    This has been covered quite a bit, but there's a crucial piece of information that's missing. Hopefully, I can help to clear up how this works and give some relief to weary travellers. :-)

    Delete From Current Process

    Obviously, everyone knows that you just do this to delete an environment variable from your current process:

    set FOO=
    

    Persistent Delete

    There are two sets of environment variables, system-wide and user.

    Delete User Environment Variable:

    reg delete "HKCU\Environment" /v FOO /f
    

    Delete System-Wide Environment Variable:

    REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V FOO
    

    Apply Value Without Rebooting

    Here's the magic information that's missing! You're wondering why after you do this, when you launch a new command window, the environment variable is still there. The reason is because explorer.exe has not updated its environment. When one process launches another, the new process inherits the environment from the process that launched it.

    There are two ways to fix this without rebooting. The most brute-force way is to kill your explorer.exe process and start it again. You can do that from Task Manager. I don't recommend this method, however.

    The other way is by telling explorer.exe that the environment has changed and that it should reread it. This is done by broadcasting a Windows message (WM_SETTINGCHANGE). This can be accomplished with a simple PowerShell script. You could easily write one to do this, but I found one in Update Window Settings After Scripted Changes:

    if (-not ("win32.nativemethods" -as [type])) {
        add-type -Namespace Win32 -Name NativeMethods -MemberDefinition @"
            [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
            public static extern IntPtr SendMessageTimeout(
                IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam,
                uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);
            "@
    }
    
    $HWND_BROADCAST = [intptr]0xffff;
    $WM_SETTINGCHANGE = 0x1a;
    $result = [uintptr]::zero
    
    [win32.nativemethods]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE,[uintptr]::Zero, "Environment", 2, 5000, [ref]$result);
    

    Summary

    So to delete a user environment variable named "FOO" and have the change reflected in processes you launch afterwards, do the following.

    1. Save the PowerShell script to a file (we'll call it updateenv.ps1).
    2. Do this from the command line: reg delete "HKCU\Environment" /v FOO /f
    3. Run updateenv.ps1.
    4. Close and reopen your command prompt, and you'll see that the environment variable is no longer defined.

    Note, you'll probably have to update your PowerShell settings to allow you to run this script, but I'll leave that as a Google-fu exercise for you.

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