4

Here're the command I've executed:

>cmd /k
>echo 1
1

>echo 2
2

>echo 3
3

>exit /b

>cmd /c "doskey /history"
echo 1
echo 2
echo 3
exit /b

>

(tested on windows 7x64) According tho the process explorer the cmd /k starts a sub-process (not a thread). So after process is exited I would expect that it will clear his things from the memory. This thing happens on 3/4/.. spawned cmds , for /f , with called batch file.

2

1 Answer 1

7

Short version:

The console window handles the command history

Detailed:

The processes of a command prompt window is as follows:

conhost.exe
--- doskey.exe
------ cmd.exe
--------- any sub-processes (In your case cmd /k)


As on Microsoft technet:

... If you exit and then restart a program from the same Command Prompt window, the command history from the previous program session is available.

You must run Doskey.exe before you start a program. You cannot use doskey command-line options from a program's command prompt, even if the program has a shell command.

Doskey is executed under conhost.exe, the process that displays the command prompt window. This enables it to monitor keystrokes for all sub-processes and threads.

When you call cmd from within another process, the root CMD process handles the doskey history


Update:

As commented by MC ND, in Windows XP/2003/Vista/2008 the command history is handled by csrss.exe. From Windows 7 upwards, conhost.exe handles the command history - more here

3
  • 4
    Just for information, for windows XP/2003/Vista/2008 the command history is handled by csrss.exe. From windows 7 conhost.exe handles this task (more here).
    – MC ND
    Commented Mar 29, 2017 at 18:13
  • 1
    But why the history is unchanged for the first cmd.exe just under doskey.exe? But when you open again a new cmd then there is the full list of all entered commands
    – jeb
    Commented Mar 30, 2017 at 19:00
  • 3
    No, it's more complicated, see doskey history behaviour across cmd instances. There are stored at least two different lists
    – jeb
    Commented Mar 31, 2017 at 12:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.