Displaying Windows command prompt output and redirecting it to a file

545,362

Solution 1

To expand on davor's answer, you can use PowerShell like this:

powershell "dir | tee test.txt"

If you're trying to redirect the output of an exe in the current directory, you need to use .\ on the filename, eg:

powershell ".\something.exe | tee test.txt"

Solution 2

I was able to find a solution/workaround of redirecting output to a file and then to the console:

dir > a.txt | type a.txt

where dir is the command which output needs to be redirected, a.txt a file where to store output.

Solution 3

There's a Win32 port of the Unix tee command, that does exactly that. See http://unxutils.sourceforge.net/ or http://getgnuwin32.sourceforge.net/

Solution 4

Check this out: wintee

No need for cygwin.

I did encounter and report some issues though.

Also you might check unxutils because it contains tee (and no need for cygwin), but beware that output EOL's are UNIX-like here.

Last, but not least, is if you have PowerShell, you could try Tee-Object. Type get-help tee-object in PowerShell console for more info.

Solution 5

@tori3852

I found that

dir > a.txt | type a.txt

didn't work (first few lines of dir listing only - suspect some sort of process forking and the second part, the 'type' command terminated before the dire listing had completed? ), so instead I used:

dir > z.txt && type z.txt

which did - sequential commands, one completes before the second starts.

Share:
545,362
Admin
Author by

Admin

Updated on July 08, 2022

Comments

  • Admin
    Admin almost 2 years

    How can I run a command-line application in the Windows command prompt and have the output both displayed and redirected to a file at the same time?

    If, for example, I were to run the command dir > test.txt, this would redirect output to a file called test.txt without displaying the results.

    How could I write a command to display the output and redirect output to a file in the Windows command prompt, similar to the tee command on Unix?

  • Kris
    Kris about 15 years
    You don't need visual studio to compile that, the commandline tools are actually free. just google ".net sdk download" for the link (the direct link seems to change around but google always seems to work).
  • VladV
    VladV almost 15 years
    Lots of Unix utilities are also ported by GnuWin32 project, see gnuwin32.sourceforge.net.
  • Leigh Riffel
    Leigh Riffel almost 15 years
    This satisfies the answer, but outputs the data after the dir command has completed rather than as the data is produced.
  • Jay
    Jay over 14 years
    Home-made tee implementation? And have to learn C#? Use tee.. really.
  • Jay
    Jay over 14 years
    That tee.bat thing looks nice. Hope OP checks this out.
  • quack quixote
    quack quixote over 14 years
    UnxUtils was last updated in 2003; GnuWin32 is a bit more up-to-date.
  • adzm
    adzm almost 14 years
    Note that using TEE.BAT will output after the command has completed, just like the "dir > a.txt | type a.txt" example posted nearby.
  • Christopher Painter
    Christopher Painter about 13 years
    That's only useful if you want to display the contents AFTER your process has run. And that isn't a hard problem to solve.
  • MTS
    MTS about 13 years
    Yeah, I guess this addresses a slightly different problem from what the original poster was asking.
  • Samaursa
    Samaursa almost 13 years
    cygwin is a pain to install. Upvote to you because this is what I was looking for.
  • mjaggard
    mjaggard over 12 years
    I don't think this outputs to the console and the files at the same time does it?
  • markmnl
    markmnl over 12 years
    why use this when there is native support in windows as per the answer below??
  • Brian Rasmussen
    Brian Rasmussen over 12 years
    @Feanor: Did you read the comment on that answer? While it works for some cases, it has it's limitation. Both answers are valid and have their uses.
  • elcool
    elcool about 12 years
    aren't you calling the same program twice with this?
  • jeb
    jeb over 11 years
    No, it's not real time, it waits until %MYCOMMAND% is finished and it fails in many cases. It skips empty lines, lines beginning with ; fails with content like <space>/<TAB>, ON, OFF or /?. But the rest could sometimes work :-)
  • djangofan
    djangofan over 11 years
    This doesn't work. I tried using this to launch the JBoss run.bat and it chokes during startup and the server freezes. There are problems with this method...
  • Davor Josipovic
    Davor Josipovic over 11 years
    Why the down vote? 95% of replies here have one thing in common: the output is redirected only after the initial command has finished: read the comments. UNIX utility tee outputs real time. wtee has the same functionality. If you don’t mind the bugs, it will do just fine.
  • Brian Rasmussen
    Brian Rasmussen over 11 years
    When down voting please leave a comment. Thanks.
  • mellamokb
    mellamokb about 11 years
    This seems to wait until the output is complete also before outputting to either the file or console.
  • Vitim.us
    Vitim.us about 11 years
    it will work, but you will get stuck if the command wait a input from stdin.
  • eduncan911
    eduncan911 almost 11 years
    +1 This is how I've always done it. I believe this is the correct answer to the original question and should be marked as so. The trick, as @MTS hints at, is that you actually write to two files: one that gets created per each command/line (hence a single ">" that overwrites each time), and then you type that line to the screen (type ), and finally, you type it again and redirect its output, all the long APPENDING, to your log file with ">>". This is how I've done it for years, though I love the simple "" temp file. I've always done "tmp.txt" or soemthing. Yeah, delete it afterwards.
  • Stoleg
    Stoleg over 10 years
    This is the most close answer: it works on default install, as PS is already there on most machines and, especially, servers.
  • Nigel Touch
    Nigel Touch over 10 years
    If you, like me, struggled to find GnuWin32's tee package, you'll find it in gnuwin32.sourceforge.net/packages/coreutils.htm.
  • ADTC
    ADTC about 10 years
    You should use & instead of && if you want to ensure the type command is executed even if dir command failed. This is useful when there was some form of error in your command and you still want to see the log file on the console. See Microsoft's article on this. However, this has the issue of %errorlevel% being set to the error level of type (which would be 0).
  • Nate Cook
    Nate Cook almost 10 years
    This is a good approach but I had problems with some errors not getting captured. Putting the type commands on separate lines (in a subroutine) fixed that though.
  • Alan
    Alan over 9 years
    Just what I needed for my application.
  • Mr. Llama
    Mr. Llama about 9 years
    If you're going to use Cygwin, it comes with tee.
  • John Demetriou
    John Demetriou over 8 years
    care to explain what's going on here?
  • Wes Larson
    Wes Larson over 8 years
    I like this simple answer! I found that an & instead of a | is needed for the output of some commands, like ping or 7z.exe
  • Sopalajo de Arrierez
    Sopalajo de Arrierez almost 8 years
    Not working for interactive commands instead of dir. Example: chkdsk /f c: > c:\Temp.txt | c:\Temp.txt . The system report file is locked by another process.
  • Shuman
    Shuman almost 8 years
    I tried this when building pyqt5.7 on windows 10, it's quite sluggish, console output is not realtime, maybe it's buffered? the log file looks correct though
  • neo
    neo over 7 years
    @raja ashok, doesn't work for me. I have tried python.exe 1>file.txt 2>&1 | type file.txt
  • Josh Werts
    Josh Werts over 7 years
    This is the best answer! Simple and works "out of the box"
  • Lii
    Lii over 7 years
    This command does not seem to handle output to stderr. Stderr seems to be printed to the console but is not saved in the file.
  • Alex
    Alex over 7 years
    That's the only solution working for me, not having Powershell > 4.0. Thumbs up!
  • Pavel P
    Pavel P about 7 years
    All of that is completely wrong. It doesn't work at all for me. The last command gives 'tee' is not recognized as an internal or external command for obvious reasons. With 2>&1 inside cmd line any output to stderr causes errors from powershell.
  • Ionut
    Ionut about 7 years
    It works, but the file will not contain all the output
  • grenix
    grenix about 7 years
    LIKE IT :) anyhow {powershell "ping -n 10 localhost | tee test.txt"} would demonstrate better whats going on on the tee party
  • Jesse Chisholm
    Jesse Chisholm almost 7 years
    @lii re: handle output to stderr - Add the 2>&1 redirection so it is like this: dir 2>&1 > a.txt & type a.txt and you should have stderr mixed in with stdout.
  • phuclv
    phuclv almost 7 years
    note that dir in powershell is an alias to Get-ChildItem which is not like cmd's internal dir command. You need powershell "cmd /c dir | tee test.txt" if you want the cmd internal version
  • phuclv
    phuclv almost 7 years
    tee on *nix is also a separate application, not some redirection on the shell
  • Zhe Hu
    Zhe Hu almost 7 years
    to see all the output, don't forget to redirect STDERR to STDOUT with 2>&1, for example node 2>&1 | tee debug_log.txt
  • Bernd
    Bernd over 6 years
    Could be a reference to the PowerShell Tee-Object cmdlet - technet.microsoft.com/en-us/library/ee177014.aspx
  • u8it
    u8it over 6 years
    I agree with @WesLarson, & or && is better than a pipe |... the pipe may not print the entire redirect for long outputs because it doesn't wait for completion, & does wait for completion and is required for delayed output.
  • Adi Prasetyo
    Adi Prasetyo over 6 years
    yeah but i got ugly whitespace, any direction?
  • JustAMartin
    JustAMartin over 6 years
    Almost, but it does not display simultaneously - if you have a long running task, you will get no results for long time.
  • dehinrsu
    dehinrsu over 6 years
    But you lose the text color on console output.
  • Ben Personick
    Ben Personick about 6 years
    I do the same when trying to capture output from a command (IE put it inside a for) but then I just call my standard subfunction to echo to the screen and write to the log because I use it throughout my script in replacement of using echo. Not sure why that got a downvote from others so I upvoted it.
  • Brent Rittenhouse
    Brent Rittenhouse over 5 years
    mjaggard: god point. It could easily be modified to do it though by adding an additional output that WAS the console.out stream.
  • R.F
    R.F about 5 years
    Is is very useful command, we we want to have everything, we need 2>&1
  • Edi
    Edi almost 5 years
    Consider adding '-Append' so the log file doesn't get truncated each command start. For example powershell "node server.js 2>&1 | tee -Append app.log"
  • Mark Deven
    Mark Deven about 4 years
    Any way this could capture the stderr output? I have an annoying exe that outputs everything on the standard error stream (i.e. in order to output to a file normally you would do file.exe 2>output.txt
  • Mark Deven
    Mark Deven about 4 years
    I figured it out. You can do it using file.exe 2>&1|tee.bat as found here: superuser.com/questions/452763/…
  • zett42
    zett42 about 4 years
    With 2>&1 any stderr output will cause a "NativeCommandError" to be displayed, intermixed with the actual output. To prevent that use powershell ".\something.exe 2>&1 | foreach {\"$_\"} | tee test.txt". This converts any error records to plain text before sending it to tee.
  • Andry
    Andry almost 4 years
  • user2956477
    user2956477 almost 4 years
    unixutils package link does not working, update package does not include tee utility. There are another tee implementations ss64.net/westlake/nt and robvanderwoude.com/unixports.php
  • Andry
    Andry almost 4 years
    @user2956477 I've added another link. I think the update package should not have all set of utilities, only those the author wants to update.
  • Pete Kirkham
    Pete Kirkham over 3 years
  • Steven Lee
    Steven Lee over 3 years
    @JesseChisholm, dir 2>&1 > a.txt & type a.txt both redirects the results to a log file and shows them on the screen. But the displaying of result to screen is delayed until the command is done. Is there a way so that the displaying of result to screen happens at the same time it is written to the log file?
  • Jesse Chisholm
    Jesse Chisholm over 3 years
    @LHC -- Only by using a utility like tee. There are various packages to get unlx-like (or linux-like) utilities that run on windows. There is no native Windows way.
  • Ryan Shillington
    Ryan Shillington over 3 years
    Cygwin tee doesn't seem to work on the dos prompt the way it works in bash. This is a nice workaround. Thank you!
  • e.d.n.a
    e.d.n.a almost 3 years
    @PavelP I guess, your issue was with the last example!? The command was missing the necessary escaping of special characters exposed to cmd. It has now been edited and should work that way!
  • Chaminda Bandara
    Chaminda Bandara over 2 years
    This is not writing to a file in windows 10
  • Aleksey F.
    Aleksey F. over 2 years
    This should be the accepted answer. wtee works on Win 2003 Srv at least, starts much faster than powersell and can be easily integrated into the building process.
  • Nikolai Ehrhardt
    Nikolai Ehrhardt over 2 years
    did not work for me: C:\Data>echo 12 > log.html | type log.html File not found error (Das System kann die angegebene Datei nicht finden.)
  • Andry
    Andry about 2 years
  • FocusedWolf
    FocusedWolf about 2 years
    @paxdiablo Just make a Compilar.bat with this line: C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe %1 and then save his code to CopyToFiles.cs and drag it onto the bat file to create the executable.
  • David Gleba
    David Gleba about 2 years
    You can install cygwin with one line like so: (setlocal enableDelayedExpansion&set packs=C:\prg\sysdata\cygpackages&mkdir !packs!&cd !packs!&echo !packs!&curl -O "https://cygwin.org/setup-x86_64.exe"&!packs!\setup-x86_64.e‌​xe --no-admin -q -n -N -d -R c:\prg\cygwin64 -l !packs!\cygwin64localpacks -s https://cygwin.mirror.constant.com -P wget). You can call a program like c:\prg\cygwin64\bin\tee.exe or You can add cygwin to the local user's path with: rundll32 sysdm.cpl,EditEnvironmentVariables. Add this path: c:\prg\cygwin64\bin