Command Prompt (cmd) is a widely used command-line tool for Windows. One of the shortcomings of CMD is that it only shows the history of commands you recently executed in the current session.
If you close the current window, all history is gone. There are multiple ways to quickly access the history of commands, clear the command history, and save your command history for future reference.
We will discuss these scenarios in this article. Please note that the commands mentioned in this article work on all versions of Windows, including Windows 7, Windows 8.1, Windows 10, and Windows 11.
Table of Contents
View Command History in Command Prompt using Doskey
To view your command history in Command Prompt, simply type the following command in your CMD window.
doskey /history
All the previously executed commands you typed in the current session will be listed on the cmd screen.
View Command History in Command Prompt using Keyboard Shortcut
You will only see the history for the current session in the Command Prompt window. Upon closing the Command Prompt window, all previous commands will be lost after restarting the program.
Once you have opened the Command Prompt window and started executing commands, Windows will save the history for your active session.
To see the list of recently executed commands in CMD, press the F7 key. This will open a pop-up inside CMD showing the list of recently executed commands. You can select any command and press enter and that specific command will be executed again.
Select the command and press the Enter button. And here you are. Your command has been executed.
Search for Commands in the Command History
You can also search for a specific command from your CMD history. To search for a specific command from the history, just press the F8 key, type whatever you remember, and then press the F8 key to cycle through the possibilities from the history.
For example, if the command I’m searching for starts with “ip“, I will type ip and then press the F8 key. Pressing the F8 key again will bring another possible result from history if any. Keep pressing the F8 key again and again until the history list ends.
Execute Commands from CMD History
When we pressed the F7 key, you should have noticed that each command starts with a number. If you want to use the command again and again, you can call it by its number by using the F9 key.
For example, if the command number 3 is the one I have to run, again and again, I will press the F9 key, type 3, and then press Enter. This will execute command no. 3 from the command history.
doskey /history
Use the Up and Down arrow keys to select the command. Or you can also Copy and Paste the commands from the history that has appeared on your screen, within the CMD window.
How to save Command Prompt History
You can only view the history of commands as long as your CMD window stays open. When you close it, all commands will be gone with no mark of history. What if you need the same commands for the next use of CMD window, but you do not know which commands you executed before?
You can save your commands list as a TXT, HTML, CSV or RTF file before closing the window so that you can have a backup of your entered commands.
Type the following command to save the command history in Command Prompt:
doskey /HISTORY > history.txt
Your file will be saved in the Users folder. Now go to the user folder and see if that has the file saved there or not.
Please check the history of all commands and save them in a notepad file to use any of your commands anytime you need.
You can also automatically save the commands you typed in the file when you exit Command Prompt.
To do so, run the following macro:
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 remaps the EXIT command to copy the command line history into your user profile folder (C:\Users\yourname\commands.log in the above example). I’ve taken this solution from serverfault. There are many other solutions for persistent history given on the page. You can check it out here.
How to Clear CMD History
Since the command history in CMD and PowerShell is not persistent, history can be cleared by closing the current CMD window. If you do not want to close the CMD window, you can also use the Alt + F7 key. This will clear the CMD history from your current session.
View Command History in Command Prompt using Clink
Clink is software that merges with the existing Command Prompt on your Windows PC. It adds extra command line features like rich command completion, cmdlet history browsing and execution, and line-editing capabilities.
These features are added by combining the existing cmd.exe file with GNU Readline libraries.
Note: Currently, Clink only works on Windows 10 and below.
Here is a complete list of the command line features which you will get in Command Prompt after installing Clink:
- The same line editing as Bash (from GNU’s Readline library).
- History persistence between sessions.
- Context-sensitive completion;
- Executables (and aliases).
- Directory commands.
- Environment variables
- Third-party tools; Git, Mercurial, SVN, Go, and P4.
- New keyboard shortcuts;
- Paste from the clipboard (Ctrl-V).
- Incremental history search (Ctrl-R/Ctrl-S).
- Powerful completion (TAB).
- Undo (Ctrl-Z).
- Automatic “cd ..” (Ctrl-Alt-U).
- Environment variable expansion (Ctrl-Alt-E).
- (press Alt-H for many more…)
- Scriptable completion with Lua.
- Coloured and scriptable prompt.
- Auto-answering of the “Terminate batch job?” prompt.
Let us now show you how to use Clink to view command history in Command Prompt:
-
Download and install Clink with default settings.
-
Run the Command Prompt.
-
Press CTRL + R shortcut keys to enter reverse search mode.
-
Enter the keywords for the command to search.
The command will then appear after the colon.
-
When the correct command appears, press Enter to run it.
Clink stores command history between sessions. This means that even if you close the window, you can search for the previously-run cmdlets in a new Command Prompt instance using the CTRL + R shortcut keys.
If you want to get rid of these extra features, all you must do is delete Clink from your PC from the Programs and Features Control Panel applet.
View Command History in PowerShell and Terminal
PowerShell can also be used in place of the Command Prompt. PowerShell can run all the commands for CMD plus many more. If you want to view the history of your commands in PowerShell, you have to type the history command. This will show a list of all recently typed commands inside the current PowerShell session.
I hope you have a clear understanding of how to view your history in CMD and how to save that history. If you have any questions, comment below to ask.
Conclusion
Although Microsoft has introduced a new command line tool in Windows, Command Prompt is still popular among users. Windows 11 comes with Terminal, a console program that integrates Command Prompt and PowerShell into the same program.
The new terminal has many features of the Linux command line, including the persistent command history. That’s a step in a positive direction. While it will take time to upgrade to the new programs, DOS should not be left alone until everyone is upgraded.
4 comments
Vibhanshu
How can we get the executed commands history if i have closed the session in command prompt?
brett
once you get to history can u delete the command prompts you want
Mikhail
I tried doskey /h in Windows 10 cmd and — it works too!
Deepak
Thank you so much for the insight, it was a very good information for me. I would like to give your command “doskey /HISTORY > history.txt” a little tweak. If we will modify the command to doskey /HISTORY >> history.txt (two arrows instead of one) this will append all the commands from the point of execution. unlike the single arrow (>) where the file will be over written when you execute the command every time.