
Here is what you put in the autoexec.bat file: off 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. Then change the value of HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\AutoRun to this: IF EXIST "%USERPROFILE%\autoexec.bat" (CALL "%USERPROFILE%\autoexec.bat") While that will work, it will definitely create unexpected side effects when doing any kind of scripting*.Ĭreate a file called autoexec.bat and store it in your profile folder (e.g., C:\Users\yourname\autoexec.bat). Anything you put in this entry will be run when you start a CMD session. The short and dangerous answer is to just add it to the REGISTRY in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\AutoRun. The tricky part is autoloading that doskey macro when you start a new command session. 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. 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).

This simple macro will save the command line history when you exit a CMD session: doskey exit=(echo/ ^& echo **** %date% %time% ****) $g$g %USERPROFILE%\commands.log ^& doskey /history $g$g %USERPROFILE%\commands.log ^& ECHO Command history saved, exiting ^& exit $*

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:Īs other answers indicated, doskey allows you to list the command line history for the current session.
