How to remove environment variable from system configuration using 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 in order to run it in order to remove system variables.

+9
windows environment-variables batch-file
Sep 24 '09 at 16:15
source share
4 answers
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
Inside the key (which looks 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), meaning (5).

Registry terminology is slightly different from msdn, but reg.exe and wikipedia seem consistent. wikipedia http://en.wikipedia.org/wiki/Windows_Registry says that "Registry values ​​are name / data pairs stored in keys" refers to a name / data pair. name (a) of data (5), and a pair together is a value.

The REG seems to use / v, which probably refers to both the name of the variable and its data (as in wikipedia), it shows both the name of the variable and its data. And REG has a / d, which I suppose is for data only.

Setx example

  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 delete it, the closest you can get cleans it like this article. MS KB this article MS50 195050 says that it leaves you with it in the registry.

setx does not remove a 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:\> 

However, it is worth noting that the variable left before the cmd prompt. echo% aaa% will display% aaa% so the variable disappears. Currently, there is only unnecessary garbage in the registry.

User

makes an interesting point that now when you try to set a system variable, it does not work. since it will be overwritten by what is in the user variable, in this case it will always be displayed as undefined.

 C:\>reg delete %uvar% /v aaa Delete the registry value aaa (Yes/No)? y The operation completed successfully. C:\> 

Note 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 this for the system / machine environment variable, not for the user.

- the lines below should be called by the administrative privileged call to cmd
-key number: HKLM \ SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Environment
-% Mvar% (unlike% uvar%) needs quotes around it, because the key has a space. If you miss the quotes, it will not be difficult for you, you will get an error that may remind you to use quotation marks.
-And of course, if you want to set the environment variable in this part of the registry using setx, use, for example, setx aaa 5 -m, i.e. -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
Perhaps you can only create an environment variable using REG ADD or REG DELETE .. (instead of setx), but in any case, when setting a variable, you can also use set set so that it is also set for the current session and not just the next . And the link is re setx https://superuser.com/questions/647505/set-enviroment-variable-setx

+11
Sep 19 '13 at 15:52
source share

The setx command with an empty string value does not delete it. And this is not just nitpicking, because the logic is completely different. Because if a user-dependent variable overrides the global variable, then it is not possible to delete the user variable and re-enable the global variable. A user-specific empty string blocks a global variable. To do this, delete DELETE (using the Evironment environment variable window).

I just tried this when the global variable JAVA_HOME was overridden by custom JAVA_HOME. It is not possible to activate the varialble global variable again without completely deleting the user variable. And so far I have not seen a way to do this from the command line or script package.

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

+4
Jul 03 '13 at 16:17
source share

You will need to modify this registry key:

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

Changes take effect after a reboot.

+3
Sep 24 '09 at 16:24
source share

If you want to avoid a reboot, you can find a way to translate WM_SETTINGSCHANGE to each active window after deleting the value.

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

0
Mar 28 '17 at 8:25
source share



All Articles