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.
To save system environment:
To save user environment:
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.
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
and no need for quotes around %h 'cos not writing setx in.
del a.bat del b.bat
Then if you could add the word set before them.
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.
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
This batch file should read the previous output and reset them for the current window: