[Looking for Charlie's main web site?]

Saving windows command prompt history to a file

Note: This blog post is from 2011. Some content may be outdated--though not necessarily. Same with links and subsequent comments from myself or others. Corrections are welcome, in the comments. And I may revise the content as necessary.
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

(Update) And in PowerShell, use:

get-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.)

Update (Nov 2017): what about in PowerShell?

In the years since this was posted (early 2011), plenty of folks have or are making the transition to Windows PowerShell (it's even becoming the default command line in some later versions of Windows). So I updated the top of this post to show how you would get the same history of commands entered into a PowerShell command line:

get-history >commands.log

If you want to learn more about that (more than get-history -help, and so on), see the brief MS doc on it.

Perhaps more compelling for some, note that PS has an even more advanced variant to this notion of saving what you do at the command prompt: how about saving BOTH the commands you type AND all their output? I'm not talking about piping the output of any one to a file, like above. Instead, this is about the PowerShell Start-Transcript cmdlet. Try it out some time:

Start-Transcript

By default it will show you that it will save all subsequent command input and output to a file in your Documents folder. But see the help or that doc page for far more control over it. The transcript will be created when you end the PowerShell session, or when you use the Stop-Transcript command.

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

For more content like this from Charlie Arehart: Need more help with problems?
  • If you may prefer direct help, rather than digging around here/elsewhere or via comments, he can help via his online consulting services
  • See that page for more on how he can help a) over the web, safely and securely, b) usually very quickly, c) teaching you along the way, and d) with satisfaction guaranteed
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.
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.
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
Try using
doskey /history | clip

The clip command puts it onto your clipboard, which them could be pasted into an editor (handy if you keep the editor open all the time)
# Posted By taybill | 9/20/12 7:15 PM
Very cool. Thanks for that, taybill.
Thank you very much, I was searching for this. :)
# Posted By NITIN HARANE | 10/30/12 5:07 AM
cool. thank you.
# Posted By Arun | 11/15/12 12:11 PM
Thanks much.
# Posted By samwel | 1/30/13 4:39 AM
Still helping out...two years later!! Thanks!
# Posted By John Watson | 4/17/13 4:41 PM
Any ideas on how to redirect input to dos DEBUG command without it allways locking up ? in other words regaining keyboard input instead the dos window/debug session freezes.

> DEBUG < sometypesavingfile.txt

cbxj
# Posted By nick | 9/3/13 10:35 AM
@Nick, sorry. I have no idea at all. I have never used the DOS command-line DEBUG feature before.
If you're like me and start cmd.exe through a shortcut with a setup batch file (a shortcut target as something like "%windir%\system32\cmd.exe /k c:\users\cdennis\Documents\winscripts\windows_auto.bat"), try adding the following to your startup batch script (code partially taken from "http://stackoverflow..."):

DOSKEY exit=DOSKEY /HISTORY $g$g "%USERPROFILE%\cmd_history.log"$t exit $*
DOSKEY eixt=DOSKEY /HISTORY $g$g "%USERPROFILE%\cmd_history.log"$t exit $*
DOSKEY exti=DOSKEY /HISTORY $g$g "%USERPROFILE%\cmd_history.log"$t exit $*

This will cause a log file to be written (as long as you close the command line by typing the command "exit", and not using the X button on the window).

The "$g" is not documented in "doskey /?", but it's from "prompt /?". However, it seems to work.

Keep in mind that there are security considerations for such a history file.
# Posted By Chris | 1/2/14 9:55 AM
Cool tip, Chris. Thanks.
Thanks for this tip - have used it numerous times over the last few years.
# Posted By Mike Steber | 10/6/15 2:37 AM
This tip also served me very well until I found out about the wonderful Clink - http://mridgers.gith...
# Posted By Ronan Mullarkey | 11/5/15 6:23 AM
Interesting, Ronan. Thanks for sharing.

So in the context of my blog post, I assume what you like most about clink is that it preserves history between command line sessions? That is indeed interesting.

So are the keyboard shortcuts, and of course the similarity to Bash for those familiar with that.

While I'd want to make sure folks seeing this do know that the Windows command line does indeed already have completion (hit the tab key while typing to get it to loop through files/folders matching any initially typed letters), I notice that clink says it's "context sensitive". I assume that means it would only show suitable things in different situations, which sounds interesting.

Hope that MS may incorporate such things some day. The Windows command line has indeed evolved over the years, incorporating more and more things from others.

Anyway, hope the tip I shared may continue to serve for a long time those who ARE still using only the Windows command line. :-)
I am running Windows 8.1 and to copy the contents of the full command prompt, I simply left click inside the window, then right click, then "select all". Then press ctrl c (which is copy) , which will copy the full text into memory. Now open word pad (or any word processor). Open a new document, then do ctrl v (which is paste). Now save the file.
# Posted By Kbguy | 1/12/16 7:58 PM
Still useful and helpful after five years. Thanks, Charlie! :-)

Regards from Spain,
-- Wayfarer
# Posted By Wayfarer | 1/28/16 4:56 AM
Thanks so much, Wayne, and glad to help. (I lament that so many people pay little attention to older blog posts. Some stuff doesn't change, and old info is as valuable today as in the past.)

I'm thinking about reprising some of those key posts (and articles and presentations) from the past, to help increase the chance of people finding them (versus solely by searching). :-)
charlie arehart is my hero of the day:

"reprising some of those key posts (and articles and presentations) from the past, to help increase the chance of people finding them"

What a great idea m8, people like you is what makes the internet great :)
I've been looking for a way to log DOS console session output to a log file, much the same as you can with all and sundry SSH clients, too. I tried this solution but it only seems to store the commands you enter, not the resultant output from it.
However, I just found that you can log all session output in Powershell using the "Start-Transcript" cmdlet:
https://technet.micr...

Maybe this is this best option (until something better comes along?). Another possible workaround is to install an SSH Server on your Windows box and open an SSH connection to "localhost"? (But I think that's a bit much since Powershell is already part of most Windows deployments now.)
# Posted By stevo | 10/8/16 2:53 AM
Thank you!
This is still helping people.
# Posted By batya | 7/2/17 3:30 AM
Hi Charlie,
What I am looking for is to save the commands and the output on the fly?
So to build a history just like putty, in there you have the option to save all commands and output on the fly to the trace/log file.
Is there such a tool available?

Thank you.
Regards,
Jerry
# Posted By Jerry | 10/1/17 6:39 AM
Hi Charlie,
What I am looking for is to save the commands and the output on the fly?
So to build a history just like putty, in there you have the option to save all commands and output on the fly to the trace/log file.
Is there such a tool available?

Thank you.
Regards,
Jerry
# Posted By Jerry | 10/1/17 6:39 AM
Jerry, it does not seem possible. See others asking and debatnig options, all fruitless, at https://stackoverflo... Don't worry about it being "in a bat". The discussions apply as well to just bring at command line, and the solutions that were suggested failed for me when I tested them, to do what you seek.

If you (or any reader) may ever find a solution for that, share it here. Seems others seek it also. :-) And this blog post (mine, here) is showing up as a top one on this topic of saving output of command line history, so others may find it and any additional info shared here on your need. I've never needed it, but I see the value.
Hey, Jerry, as an update to my last comment: you CAN get what you want (a transcript of your command input AND output), at least if you use Windows PowerShell instead. (Thanks to Stevo who actually shared the news in a comment last year, that I somehow missed responding to.)

I have just updated the blog post to discuss that at the end, and its available Start-Transcript, which will log both the input and output. (I also updated the top to show how to get JUST the command history in PowerShell.)

The transcript feature is worth looking into, even if you may not be a user of PowerShell. Sometimes you can do exactly what you do at the Windows/DOS command prompt, in PowerShell. (And sometimes you can't, but it's worth learning to deal with the differences, and to embrace the many, many, many powerful advantages of PowerShell over the command prompt.) But I realize many will cling to the simpler cmd for a long time, and so I hope this post may help folks of either stripe.
Still helping out...8 years later!! Thanks!
# Posted By Ranjeet | 4/10/19 7:10 AM
Thanks, Ranjeet. Happy to help: and yep, even old content can still be great content--especially when it's about something so many people don't know or might not ever even fathom. :-)
Best post I have seen on this topic, truly awesome. Previously have used Start-transcript in PowerShell to record commands/output and pipping in command prompt.
It may be old-hat by now, but I just wanted to add that you can also switch to command prompt while in PowerShell by entering the command "cmd", which then will let you execute some of those commands that will only work in the command prompt. Not totally sure they will all work, but I have not hit a snag yet. It also records those commands if you used the Start-Transcript prior to running the command. Thanks again for the great source.
# Posted By James Brown | 10/8/20 10:34 AM
Thanks very much, James--especially nice to hear that for such an old post, but it is indeed proving to be a classic and one of my most popular. Glad to have helped.

And very nice on that tip regarding running cmd in PS, and how the start-transcript will record it also. As more and more people move to PS, and yet may still be stuck saying, "I know I could do this trivially in CMD", it will be nice if it's that easy to switch.

Of course, now with the new and ever-improving MS Terminal, it's also simple for us (who may like that freedom to switch back and forth) to open a new tab as CMD or PS, etc.

The world's evolving, but both new and old stuff can be equally compelling, when we have the right mindset. And if you don't have a blog, I hope you'll consider it. I sense you'd have good stuff to share yourself. :-)

(I know some people prefer to share discoveries on twitter, slack, etc. these days, but note how Google won't readily find those like it will even a years-old blog post that has the exact solution one needs.)
Copyright ©2024 Charlie Arehart
Carehart Logo
BlogCFC was created by Raymond Camden. This blog is running version 5.005.
(Want to validate the html in this page?)

Managed Hosting Services provided by
Managed Dedicated Hosting