[Looking for Charlie's main web site?]

Saving windows command prompt history to a file

Do you find yourself working at the Windows (DOS) command prompt window (aka Start>Run>cmd), and after having entered many commands, wish you could save them to a file, such as before closing the window or perhaps when needing to restart?

This is a bit of esoterica, but as I've seen some searching for a solution in various help forums, I figured I'd share it here. It can be especially useful if you've been using the Microsoft LogParser command-line tool, which allows you to use SQL statements from the command line to analyze log files of all sorts. You may build up a large set of them during a session, and wish you could save them off before closing the command prompt window.

Quick Answer:

doskey /history > commands.log

Explanation

This solution and its meaning will be old hat to some, but it seems to be a revelation to others (judging by how many I found searching for a solution to this challenge, and not being offered any useful answer).

Using the Command History

First, some may know that you can recall past commands on the command prompt (to re-execute them) using the up arrow. You can also "see" a list of the past commands (to choose from them, to re-execute) using f7. But that old-school dos popup can't be edited or saved in any way.

Well, all this command-line history goodness is really driven by a command that's executed implicitly when you open a command prompt window, called DOSKEY.

And if you enter doskey /?, you get some available help (including reminders of the shortcut keys above, and more). But you will see that it has the /history argument I've used, and that lists (to the screen) all the past commands you've entered during the current command prompt session.

Of course, from there, if the list is small enough to appear all on screen, you can just copy/paste the stuff to some file (if you know how to copy content from/to the command window), but if it scrolls off screen, that's where the command I offered is most handy (yes, I know about the "more" command to cause paging of DOS command output, but really, I'd sooner use the one command above then doing a copy, then page forward, and copy. To each his own.)

Piping the history to a file

So saving the displayed history to a file just involves a little more old-school DOS trickeration, whereby you can redirect the output of any command to a file, using the ">" argument, and naming the file to hold the output. The named file will be created in the current directory (indicated in the command prompt window).

Beware, though, that that command will overwrite any previous content in that file. If you want to append to it, instead, use ">>" in the command above. This could be useful if you, for instance, wanted to always write the log to some file, such as in your drive root, so it would become:

doskey /history >> \commands.log

Just be really careful you don't forget those two brackets, or you'll lose what you have!

I instead write it to a file (not worrying where it is), then open it and save that off to evernote. (There may even be some trick to route the save directly to evernote, but I don't know that.)

More on Doskey and command prompt power

For more info on fun with this command prompt doskey and related features, see these docs, which while for XP are good for Vista/Windows 7 (I couldn't find the same content in a more updated page at Microsoft.)

Hope that helps someone. (Just couldn't fit it in a tweet. Darn!)

Comments
Hey Charlie,

Thanks for the great post. How would I do this automatically at the end of each command session? I'm concerned about malware being installed via the command line and want to see a cumulative history for forensic purposes.

Thanks!
# Posted By Kevin | 2/14/11 2:04 PM
Kevin, as for causing an automatic dump of the commands at the close of a command window, there may well be a way to do it but I'm not aware of any. Sorry I can't help more there.
# Posted By Charlie Arehart | 2/15/11 11:54 AM
Very nice sir! I am used to being on Linux at home and I usually set my history to an amount that I am assured I wont lose what I want and since it saves my history when the terminal is closed I never thought of piping this to a file. If I can't have it just retain the history after the session is closed this is the next best thing. Thank you.
# Posted By Josh | 2/22/11 4:15 PM
Hi Charlie, I run an interactive JAVA program on the DOS and want to backup the whole interaction in a text file. For instance in ORACLE SQL PLUS prompt, you get everything spooled, when spooling. Commands as well as the output. Is there a way to achieve it in DOS ?

regards
Skt
# Posted By Sat | 9/17/11 10:41 AM
Skt, I can't imagine how that may be possible, no. Well let me put it this way: I don't see it connecting to the focus of this entry (tracking history of the command prompt). Instead, for your need I'd think you'd have to hope the tool had some way to log things itself.
# Posted By Charlie Arehart | 9/17/11 12:07 PM
Tried using schtasks to record the history, the problem is that it spawns a brand new doskey command, so it doesn't actually get the command history of the original command prompt. I guess just leave your windows open and run Charlie's cool command, run a keystroke logger, or poke around some more...
# Posted By Scott Lucas | 3/21/12 12:59 PM
Maybe I'll try powershell too...
# Posted By Scott Lucas | 3/21/12 1:01 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.005.

Managed Hosting Services provided by
Managed Dedicated Hosting