Save command history of Windows cmd

Sandip Chitale
2 min readDec 18, 2020

--

Yes you can!

Windows cmd supports command history using doskey . Unfortunately the history is not remembered across sessions. This is frustrating especially when you have used complex set of commands, which you can recall in the same session using up and down arrow keys or F7 popup, only to loose them when the cmd window is closed. Here is a somewhat weird attempt at saving and restoring the command history. It uses the following 5 doskey macros:

Macros to save and load command history in cmd
  • The _ macro saves the current command history to a file cmd.history in users home directory. Of course to save the history you have to rememeber to invoke the _ macro.
  • The + macro displays the current command history saved in file cmd.history
prompt $g
cls
dir
netstat -anop tcp
ipconfig
cls
  • The . macro puts the history in the clipboard like:
> break||(
More? prompt $g
More? cls
More? dir
More? netstat -anop tcp
More? ipconfig
More? cls
More? )

You can now paste the contents of the clipboard in your cmd window. Huh? Well using this syntax you can paste these commands but they will not be executed. Yet if you use up and down arrow keys, the commands in bold will be recalled. You can ignore the bogus break||( and ) commands that get introduced. Weird huh? I told you.

  • The _u collapses the duplicate entries.
  • The (dash) macro edit the cmd.history file.

You can save the above macros in doskey.mac file somewhere and activate them using the following command:

> doskey /MACROFILE=doskey.mac

Some macros use linux utilities awk, grep and sed available via wsl.

--

--

No responses yet