Keep cmd.exe command history between sessions?
Solution 1:
Switch to using PowerShell, and follow the instructions at the following site to enable history:
https://devblogs.microsoft.com/powershell/perserving-command-history-across-sessions/ (archived)
Alternatively, in cmd.exe, you can use "doskey /history" at the end of your session to show what you typed in that session, but theres no way to really load it into the next session.
Solution 2:
I've found 2 ways, neither of which require switching to PowerShell.
-
Install Clink, which enhances
cmd.exe
with persistent history and much more. Just install it and then opencmd
as normal. -
Install TCC/LE free version, which is a separate program, again providing an enhanced version of
cmd.exe
.
Solution 3:
Saving history is a small workflow - here's a less "heavy" way to do this (no external libs).
Create a bat/cmd file to set up your history, in this case I called it MyEnvironment.cmd:
doskey save=doskey /history $g$g C:\CmdHistory.log
doskey quit=doskey /history $g$g C:\CmdHistory.log $T exit
doskey history=find /I "$*" C:\CmdHistory.log
cls
Then run this from "Start->Run" (you can also setup an alias for this too):
cmd.exe /K C:\MyEnvironment.cmd
Every time I'm closing a session I hit "quit" - or if I'm afraid of losing history mid-session I hit "save". If I want to grep for something in history, I just hit "history KEYWORD".
Per @dave_thompson_085 's comment, the AutoRun
feature works well if you don't want to use the /K
switch. If you set up the Registry key correctly, the .cmd
or .bat
does not need to be in %AppData%
, it can be in the same location it already is.
If you do use the %AppData%
location, be aware that cmd will probably look for your batch file in the "Roaming" folder (instead of the AppData root).
More info on the AutoRun
CMD feature: https://superuser.com/a/302553/333316
Update (Oct. 2020) on Autorun: I've used this feature now for years, but would like to point out many programs that rely on CMD to run background commands (Visual Studio, Win32 apps), get very confused if anything runs before the passed CMD command (e.g. VS developer prompt). If this causes pain, switch to the /K
method.
Solution 4:
I use cygwin. It also provides some others functionalities that Linux has but Windows not.