Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6856229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:51:32+00:00 2026-05-27T01:51:32+00:00

If I start a new CMD shell from an existing shell, the new shell

  • 0

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!
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:51:33+00:00Added an answer on May 27, 2026 at 1:51 am

    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:

    @echo off
    setlocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
    goto main
    
    :SetFromReg
    FOR /F "tokens=2,*" %%A IN ('REG query "%~1" /v "%~2"^|find /I "REG_"') DO (
        call set %~3=%%B
    )
    goto :EOF
    
    :GetRegEnv
    FOR /F %%A IN ('REG query "%~1" /s^|find /I "REG_"') DO (
        if /I not "%%~A"=="Path" call :SetFromReg "%~1" "%%~A" "%%~A"
    )
    goto :EOF
    
    :InheritOrDelete
    for %%A in (save_TEMP Path SystemRoot SystemDrive ProgramFiles CommonProgramFiles ALLUSERSPROFILE COMPUTERNAME LOGONSERVER USERNAME USERDOMAIN HOMEDRIVE HOMEPATH USERPROFILE APPDATA) do if /I "%%~A"=="%~1" goto :EOF
    set %~1=
    goto :EOF
    
    :main
    REM Save temp
    set save_TEMP=%temp%
    if not defined save_TEMP set save_TEMP=%tmp%
    
    for /F "delims==" %%A in ('set') do call :InheritOrDelete "%%~A"
    call :GetRegEnv "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
    call :GetRegEnv "HKCU\Environment"
    
    REM Special handling for Path
    call :SetFromReg "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" Path Path
    setlocal
    set u=
    call :SetFromReg "HKCU\Environment" Path u
    endlocal&if not "%Path%"=="" if not "%u%"=="" set Path=%Path%;%u%
    
    REM Restore TEMP/TMP
    set TEMP=%save_TEMP%
    set save_TEMP=
    set TMP=%TEMP%
    
    REM start some command...
    start cmd /d /k set
    

    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:

    'orgenvshell.vbs:
    Set WShl = CreateObject( "WScript.Shell" )
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    Function CanInherit(n)
        CanInherit = False
        w = Split("SystemRoot SystemDrive ProgramFiles CommonProgramFiles ALLUSERSPROFILE COMPUTERNAME LOGONSERVER USERNAME USERDOMAIN HOMEDRIVE HOMEPATH USERPROFILE APPDATA")
        For Each i In w
            If 0 = StrComp(i,n,1) Then
                CanInherit = True
                Exit Function
            End If
        Next
    End Function
    
    Function GetShortFolderPath(p)
        GetShortFolderPath = p
        On Error Resume Next
        GetShortFolderPath = FSO.GetFolder(p).ShortPath
    End Function
    
    Sub E(dst,src)
        set envs = WShl.Environment(src)
        For Each i In envs
            t = Split(i,"=")
            n = t(0)
            If n = "" Then n = "="&t(1)
            If IsNull(dst) Then
                If not CanInherit(n) Then envs.Remove n
            Else
                v = Mid(i,Len(n)+2)
                envd = dst
                If "X" = dst Then
                    v = WShl.ExpandEnvironmentStrings(v)
                    envd = src
                    If 0 = StrComp(n,"TMP",1) Then v = GetShortFolderPath(v)
                    If 0 = StrComp(n,"TEMP",1) Then v = GetShortFolderPath(v)
                End If
                WShl.Environment(envd)(n) = v
            End If
        Next
    End Sub
    
    E Null,"PROCESS"
    E "PROCESS","SYSTEM"
    E "PROCESS","USER"
    E "X","PROCESS"
    
    'Special hack for Path
    s = WShl.Environment("SYSTEM")("Path")
    u = WShl.Environment("USER")("Path")
    If Len(u) Then s = s&";"&u
    WShl.Environment("PROCESS")("Path") = WShl.ExpandEnvironmentStrings(s)
    
    'Test a command
    WShl.Run "cmd /d /k set ",1
    

    You could probably remove a lot of the white-listed items by querying WMI and using other WSH methods…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm going to start a new project - rewriting an existing system (PHP +
Is there a way I can open a new cmd window and pass a
Whenever I start a new project in Flex builder I always have to go
I have to start a new project where user authentication/management will be required. A
I'm using start cmd.exe /c to start new command line processes in the background.
I'm attempting to start a process from a piece of code but I want
I'm starting a shell script (.cmd) from my .NET application. I'd like the console
I will start new PDA project on the windows mobile and compact framework 2.0
The following code causes a NullReferenceException tStartParameter = String.Format(tStartParameter, tTo, tSubject) tProcess = Process.Start(New
Start a new Silverlight application... and in the code behind (in the Loaded event),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.