user73628
user73628

Reputation: 3765

How to remove an environment variable from the system configuration with a batch file

I need to remove system variables from client workstations. I have more than 500 clients, so I want to provide a batch file to the user to run himself/herself to delete the system variables.

Upvotes: 10

Views: 45671

Answers (4)

user1921254
user1921254

Reputation: 61

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 DELETE it (using the environment variables window) to achieve that.

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 variable 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.

(Not enough reputation points to add comment to the above answer about setx, so have to add my own answer...)

Upvotes: 5

Eric J.
Eric J.

Reputation: 150148

You will need to modify this registry key:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment

Changes are effective after rebooting.

Upvotes: 4

Dan P.
Dan P.

Reputation: 11

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.

That will make new CMD windows notice that the variable has been deleted (and any other program that understands WM_SETTNGSCHANGE).

Upvotes: 0

barlop
barlop

Reputation: 13790

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

Upvotes: 12

Related Questions