How do I clear all environment variables from a Windows shell session?

12

1

If I start a new Windows shell session and type:

env

I see lots of environment variables set. I want to debug the running of a command line program by removing all environment variables from the current shell session.

I could go though one by one with the following approach:

SET FOO=
SET BAR=
SET ... ... ...

However is there a simple way to clear them all in one go?

TiGz

Posted 2011-05-04T14:58:07.883

Reputation: 255

1What is env? I've never heard of that before, and it does not work here. – paradroid – 2011-12-05T12:44:56.200

Answers

13

You can write this in a batch file:

@echo off
if exist ".\backupenv.bat" del ".\backupenv.bat"
for /f "tokens=1* delims==" %%a in ('set') do (
echo set %%a=%%b>> .\backupenv.bat
set %%a=
)

It basically runs through each environment variable, backs them up to a batch file (backupenv.bat) and then clears each variable. To restore them you can run the backupenv.bat file.

Gaff

Posted 2011-05-04T14:58:07.883

Reputation: 16 863

I suggested avoiding the temporary file by using in ('set') do (answer updated) – Jan Wilmans – 2019-11-14T19:49:52.323

This is great. Thanks! One small thing: the backupenv.bat file seems to have 2 trailing spaces at the end of every SET A=B line. This seems to screw up some variables on restoring. – TiGz – 2011-05-04T15:58:54.793

Removed the space following %%a=%%b, see the edited answer. Should be fine now. – Gaff – 2011-05-04T16:04:33.877

That got rid of one of the two spaces... one space remains at the end of each SET line. Which is weird. – TiGz – 2011-05-04T16:13:04.800

Perhaps the space was added from when you previously ran my (slightly broken) script, and you ran the batch to restore... thus adding a space to each variable? Then you ran it again and in the output it would have two spaces? If this is the case then sorry for previously having suffixed all your variables with a space. Should be easy to clean up though. – Gaff – 2011-05-05T00:54:23.740

Nah - I was opening a new shell each time. Try it... do you not end up with an extra space? – TiGz – 2011-05-05T09:25:27.207

You're right @TiGz - sorry about that. I've updated the answer which should fix the problem, no more trailing spaces! – Gaff – 2011-05-05T11:00:56.930

6

If you only want to clear all environment variables for the batch file, it gets even easier.

@echo off
Setlocal enabledelayedexpansion
Set >set

For /F "tokens=1* delims==" %%i in (set) do set %% %i=
Del set
Set

The final line outputs all the environment variables, which should be none, confirming the code worked.

Because Setlocal is used, all environment changes are lost after the batch ends. So if you type Set after the batch, you'll see all the environment variables are still there hence no need to store it in a backup file.

Aaron Lowe

Posted 2011-05-04T14:58:07.883

Reputation: 61

Please make some effort to use code formatting. It makes things a lot more readable. – boxspah – 2016-03-26T19:26:22.443

accepted answer is better than this as the other answer has a way to backup existing – SeanClt – 2016-03-26T20:16:06.523

@SeanClt You need to read my full comment. It's says that backup is not needed in this case. – Aaron Lowe – 2016-03-26T22:47:54.837

@scripthero I am currently unable to format posts, sorry – Aaron Lowe – 2016-03-26T22:49:38.830

Yep you are right – SeanClt – 2016-03-27T01:47:50.383