Yes, it's possible
If all you want to do is save your command line history at the end of every session, here's a simple way to do it:
As other answers indicated,
doskey allows you to list the command line history for the current session. It also allows you to create macros.
This simple macro will save the command line history when you exit a
doskey exit=(echo/ ^& echo **** %date% %time% ****) $g$g %USERPROFILE%\commands.log ^& doskey /history $g$g %USERPROFILE%\commands.log ^& ECHO Command history saved, exiting ^& exit $*
This creates a macro that remaps the
EXIT command to copy the command line history into your user profile folder (e.g.,
C:\Users\yourname\commands.log). Unfortunately, you won't be able to easily go back to any of these entries by pressing the up arrow, but you can examine the file at any point to see what you did in the past.
So that's the easy part. The tricky part is autoloading that doskey macro when you start a new command session. The short and dangerous answer is to just add it to the REGISTRY in
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\AutoRun. Anything you put in this entry will be run when you start a
While that will work, it will definitely create unexpected side effects when doing any kind of scripting*.
So here's how I do it:
Create a file called
autoexec.bat and store it in your profile folder (e.g.,
C:\Users\yourname\autoexec.bat). Then change the value of
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\AutoRun to this:
IF EXIST "%USERPROFILE%\autoexec.bat" (CALL "%USERPROFILE%\autoexec.bat")
So yes, this batch file will run every time a
CMD session is opened, but we'll do a trick to make sure it doesn't run in any subprocesses. Here is what you put in the
if "%AUTOEXEC%" EQU "1" goto :eof
echo Loading macros.
rem remap exit command to save a copy of the command line history to a log before exiting.
DOSKEY exit=(echo/ ^& echo **** %date% %time% ****) $g$g %USERPROFILE%\commands.log ^& doskey /history $g$g %USERPROFILE%\commands.log ^& ECHO Command history saved, exiting ^& exit $*
How does this avoid those unexpected side effects?
All we're doing is creating an environment variable called
AUTOEXEC. Then, when any subsequent subprocesses open a command session, they will run into the second line and immediately exit the script! This is because subprocesses automatically inherit the environment variables from the parent process.
But wait, there's more!
This allows you to add as many macros as you want to your command session. For example:
rem review previous command line entries:
DOSKEY history=notepad %USERPROFILE%\commands.log
rem copy the current directory to the clipboard
DOSKEY cc=cd^|clip ^& echo %%CD%% copied to clipboard
Now you've got all the macros you can eat, and you're saving your command line session each time. But remember, if you don't explicitly type
exit, the history won't be saved. Closing the window with the mouse won't work here.
Hope this helps!
* because batch subprocesses often open a second copy of
CMD which will run this command again and again, from slowing everything down to other unforseen issues.