Take the 2-minute tour ×

How do I write a batch file that backs up and restores the environment variables.

After that, I want to set some of the variables and if something goes wrong, I can rollback the changes with this created batch file.

share|improve this question
What operating system are you using? –  uSlackr Sep 19 '11 at 12:52
it is windows xp –  picknick Sep 19 '11 at 12:53
add comment

3 Answers

Use reg system tool:

To save system environment:

reg save "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" env_sys_backup.hiv /y

To save user environment:

reg save "HKCU\Environment" env_usr_backup.hiv /y

To restore:

reg restore "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" env_sys_backup.hiv
reg restore "HKCU\Environment" env_usr_backup.hiv

See reg /? for help.

share|improve this answer
hmm, that kind of method looks logical.. I haven't tried it but it looks safe and logical –  barlop Oct 23 '11 at 0:51
Great solution. But you need to add the reg key for the restore: E.g. reg HKCU\Environment restore env_usr_backup.hiv Then it works, I tried it on Windows 7 to Windows 2008. –  Meir Jan 10 '13 at 10:16
@Meir Thanks! Fixed. –  Piotr Dobrogost Jan 10 '13 at 12:14
add comment

If you are running Windows, you cannot set the global environment variables from within a batch file without additional software. They must be set through the Control Panel, System applet in order to make them global.

As for capturing them, using SET >env.txt will capture them.

This batch file should read the previous output and reset them for the current window:

for /f %%a in (env.txt) do (
set %%a


share|improve this answer
pretty good... though you can use setx to set global. setx var value -m I guess your batch file could need a little amendment then, if using that –  barlop Sep 19 '11 at 13:53
setx isn't part of windows 7 or XP that I'm aware of. Is it a resource kit utility? –  uSlackr Sep 19 '11 at 13:55
yeah it's in the wik2k resource kit works for any NT microsoft.com/download/en/confirmation.aspx?id=4200 –  barlop Sep 19 '11 at 13:57
add comment

It sounds a little dangerous to me. SET you can't go far wrong with, SET >file will back up your variables, but to restore them all requires some acrobatics and seems a little bit risky, and overkill really.

Why restore them all if only some are bad.

Things can go wrong with setx..

The reasons for the acrobatics, are that SET doesn't distinguish between user variables and machine/system variables. There is REG QUERY which can help to display environment variables instead of SET, with some skill. And it's not risky, just takes some skill.

Furthermore, you might think that since SET displays VARIABLE=VALUE that if you used VALUE in setx, it'd put that value in. But setx has some funny behaviour, in that if you do

set ggg=r"r <-- that sets ggg to r"r

but if you do

setx ggg r"r <-- that sets ggg to rr , and if you want r"r you have to do \"

Maybe setx eating quotes(i mean, within the value) is not a bad thing 'cos you probably don't want any quotes in there anyway. But worth knowing what's going on.


set ggg=rrr sss <--- sets ggg to rrr sss

setx ggg rrr sss <--- sets ggg to rrr

so setx need quotes around the value. If you didn't know that, and you tried to use setx to replace ALL your environment variables, you could go wrong.

so it requires quotes around the value(in obvious cases i.e. value has spaces) and eats quotes within the value. And also when using -m (which you do for system variables), it needs it at the end. If you put it at the beginning it takes it as the variable value being -m

all of that is not obvious.

and it's setting variables permanently.

So I personally wouldn't want to use setx for every single variable put out by SET in an automated thing, it's just not necessary, and if I did i'd want it heavily tested..

And if I use it on one variable I tend to check that it worked because I might have made an error, it may have a quirk, and it's permanent.

Personally, i'd back up variables, like with SET.. or SET | find /i "path" >a Before doing setx on path..

But wouldn't try to use setx on what is dumped from SET.. or from REG QUERY.. 'cos I wouldn't want to permanently change variables that are fine. lots of variables when maybe one is funny.

If you're not writing batch files to change all your environment variables, then you're not likely to be getting all your variables wiped or messed up!

What you might want to do, is make a bat file with the individual setx command for the variables you want to replace, and back that up

I suggest these set mvar=HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment setx mvar "%mvar%" set uvar=HKCU\Environment setx uvar "%uvar%"

All that said,

and still don't go doing things in one go..

here are 2 for /f commands that'd help you generate 2 bat files, one for your user variables, one for your environment variables, so help you god.

:: delete a.bat and b.bat so it doesn't append onto what's already there  
del a.bat
del b.bat 

C:\>for /f "tokens=1,2,3" %f in ('reg query "%uvar%" ^| tail -n +5') do @echo setx %f "%h" >>a.bat

C:\>for /f "tokens=1,2,3" %f in ('reg query "%mvar%" ^| tail -n +5') do @echo setx %f "%h" -m >>b.bat

then edit them and make sure they only have what you want.. and you don't see anything funny.

you'd probably be better off not doing it like that, and.. you know what you could do.. is backup all system variables and user variables but without saying setx, so you have them backed up without the scaryness of doing sex setx on all of them.

and no need for quotes around %h 'cos not writing setx in.

del a.bat del b.bat

C:\>for /f "tokens=1,2,3" %f in ('reg query "%uvar%" ^| tail -n +5') do @echo %f %h >>a.bat


Then if you could add the word set before them.

C:\>for /f "tokens=1,2,3" %f in ('reg query "%uvar%" ^| tail -n +5') do @echo set %f=%h >>a.bat


Run those.

See if things function OK..

then maybe if you want to set all the environment variables permanently, presumably when it's unnecessary to (since it'd almost never be necessary). Then you could change from the set version of the for, to the setx version.

I'm not going to write a bat file to do that because it's not well tested, but that's everything that'd need to be in any batch files.

Put it together at your own risk, and I suggest when you do, you test it well, like on a virtual machine maybe.

share|improve this answer
add comment

Your Answer


