28

I'd like to easily save the log of the Windows command prompt into a file. Is there any alternative to select everything and right-click on copy?

Kara
  • 6,115
  • 16
  • 50
  • 57
Luis Andrés García
  • 5,852
  • 10
  • 43
  • 57

3 Answers3

56

You can redirect the output of a cmd prompt to a file using > or >> to append to a file.

i.e.

echo Hello World >C:\output.txt
echo Hello again! >>C:\output.txt

or

mybatchfile.bat >C:\output.txt

Note that using > will automatically overwrite the file if it already exists.

You also have the option of redirecting stdin, stdout and stderr.

See here for a complete list of options.

Bali C
  • 30,582
  • 35
  • 123
  • 152
11

First method

For Windows 7 and above users, Windows PowerShell give you this option. Users with windows version less than 7 can download PowerShell online and install it.

Steps:

  1. type PowerShell in search area and click on "Windows PowerShell"

  2. If you have a .bat (batch) file go to step 3 OR copy your commands to a file and save it with .bat extension (e.g. file.bat)

  3. run the .bat file with following command

    PS (location)> <path to bat file>/file.bat | Tee-Object -file log.txt

This will generate a log.txt file with all command prompt output in it. Advantage is that you can also the output on command prompt.

Second method

You can use file redirection (>, >>) as suggest by Bali C above.

I will recommend first method if you have lots of commands to run or a script to run. I will recommend last method if there is only few commands to run.

Mr.Hunt
  • 4,833
  • 2
  • 22
  • 28
  • NB: The second method is for the Visual Basic command window, not Windows Command Prompt – Adam Burley Dec 01 '14 at 12:04
  • 1
    @Kidburla- No, file redirection using `<` or `>>` work on the Windows Command Prompt. – weir Feb 07 '17 at 15:11
  • is there a trick so that it can self-limit the size of the file.. I mean say let the log be 1mb, no more... so only at max. the most recent 1mb of lines is ever stored at one time? – ycomp Jun 13 '17 at 04:03
1

In cmd when you use > or >> the output will be only written on the file. Is it possible to see the output in the cmd windows and also save it in a file. Something similar if you use teraterm, when you can start saving all the log in a file meanwhile you use the console and view it (only for ssh, telnet and serial).

oscar_ceja
  • 11
  • 1