Command line to remove environment variable from OS level configuration

Windows has a setx command

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

So you can set such a variable

 setx FOOBAR 1 

and you can clear a value like this

 setx FOOBAR "" 

However, the variable is not deleted; it remains in the registry

foobar

So, how would you actually delete the variable?

+152
windows environment-variables registry
Nov 04
source share
5 answers

To remove a variable from the current environment (not permanently):

 set FOOBAR= 

To permanently remove a variable from the user's environment (which is setx ):

 REG delete HKCU\Environment /F /V FOOBAR 

If the variable is set in the system environment (for example, if you originally set it using setx /M ), as an administrator start:

 REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V FOOBAR 
+175
Nov 04 '12 at 20:52
source share

To remove a variable from the current command session without permanently deleting it, use the regular built-in set command - just don’t put anything after the equal sign:

set FOOBAR=

To confirm, run set with no arguments and check the current environment. The variable must not be in the whole list.

Note : this will only remove the variable from the current environment - it will not be stored in the registry. When a new batch process starts, the variable will return.

+72
Jan 27 '14 at
source share

Agree with CupawnTae.

SET not suitable for changes in the master environment.

FYI: system variables are located in HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment (much longer than user-defined vars).

The full command for the system var named FOOBAR is: REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V FOOBAR (note the quotation marks needed to process the space)

Too bad, the setx command setx not support delete syntax. setx

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

+13
May 14 '14 at 20:28
source share

The specified command does not work, but this happened:

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

The HKLM label can be used for HKEY_LOCAL_MACHINE .

+11
Nov 26 '14 at 19:44
source share

setx FOOBAR "" just causes the FOOBAR value to be an empty string. (Although, this shows with the set command with "", so perhaps double quotation marks are a string.

I used: set FOOBAR =

and then FOOBAR is no longer specified in the set command. (No shutdown required).

Win7 32bit, using the command line, non-admin is what I used. (There is no cmd or Windows-r, which may differ.)

By the way, I did not see the variable that I created somewhere in the registry after I created it. I am using regedit not as admin.

+2
Jun 05 '17 at 23:33
source share



All Articles