If I start a new CMD shell from an existing shell, the new shell inherits the existing environment. Is there a way to start a new shell but have it initialized to the system defaults and not inherit?
Current result:
B:\>set _test=blooharky
B:\>cmd
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
B:\>set _
_test=blooharky
Desired result:
B:\>set _test=blooharky
B:\>cmd /env=default
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
B:\>set _
Environment variable _ not defined
[update] Solution for this is start /i cmd, as shared by dbenham, below. However it doesn’t help in the situation where the current shell is already second generation. Example:
d:\>set _
Environment variable _ not defined
d:\>set _test=blooharky
d:\>cmd /k
:: some work done using _test here...
:: ...but after we need a new clean shell:
d:\>start /i cmd
d:\>set _
_test=blooharky
:: uhoh, our shell isn't clean!
Some of the variables are initialized at logon and are not stored with the other registry entries so if you want a environment that is equal to the initial explorer.exe environment you need to white-list those items and hope they have not been changed by anyone:
The output from reg.exe is not the same on every windows version so you would have to make sure that it works on your target system (It will also have problems if the name of a variable contains a space, you could fix that by replacing
"tokens=2,*"with"tokens=2,* delims= "(delims equals tab) but before you do that make sure that the reg.exe output always uses tab as the separator)You can work around these issues by using a windows scripting host script instead of a batch file:
You could probably remove a lot of the white-listed items by querying WMI and using other WSH methods…