Take the 2-minute tour ×

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?

share|improve this question
And please stop calling it MSDOS! The similarities between cmd.exe and that braindead command.com are minuscule, and getting smaller. –  paxdiablo Apr 28 '09 at 7:06
possible duplicate of How do I echo and send console output to a file in a bat script? –  Joshua Drake Sep 19 '13 at 15:26
None of these work if one has a console application that loads a dll that outputs text. The main application text gets redirected to a file but the output from the dll does not and continues to be displayed in the console window. I have found NO way to capture the text from the dll. –  Brian Reinhold Sep 25 '13 at 19:30
one could still just pipe into a file and use tail (stackoverflow.com/questions/187587/…) on that file –  x29a Mar 19 at 12:36

23 Answers 23

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.

share|improve this answer
This satisfies the answer, but outputs the data after the dir command has completed rather than as the data is produced. –  Leigh Riffel Aug 17 '09 at 21:23
it will work, but you will get stuck if the command wait a input from stdin. –  Vitim.us Apr 18 '13 at 16:03

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

share|improve this answer
The link points to a Windows implementation of the Unix command tee, so it does work on Windows. –  Brian Rasmussen Apr 28 '09 at 6:50
Yeah, voting up the answer and the comment. I'd actually prefer CygWin since it has everything but some people prefer non-DLL tools that they can pick and choose. –  paxdiablo Apr 28 '09 at 7:10
Lots of Unix utilities are also ported by GnuWin32 project, see gnuwin32.sourceforge.net. –  VladV Jul 17 '09 at 7:16
UnxUtils was last updated in 2003; GnuWin32 is a bit more up-to-date. –  quack quixote Feb 17 '10 at 11:26
If you, like me, struggled to find GnuWin32's tee package, you'll find it in gnuwin32.sourceforge.net/packages/coreutils.htm. –  Nigel Touch Feb 18 at 18:10

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.

share|improve this answer
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. –  davor Sep 27 '12 at 16:49
+1 for wintee. Simple and works like a charm. –  Jake Stoeffler Sep 27 '12 at 17:10
+1 - simple, works, AND open source –  sage Mar 8 '13 at 16:11
Tee-Object in PS works fine –  Seb Boulet Aug 23 '13 at 12:28
+1 - Very useful. Easy to use. –  Wazza Jun 16 at 7:16


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.

Good to find this stuff, appreciated.

share|improve this answer
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). –  ADTC Apr 2 at 4:29

A simple C# console application would do the trick:

using System;
using System.Collections.Generic;
using System.IO;

namespace CopyToFiles
    class Program
        static void Main(string[] args)
            var buffer = new char[100];
            var outputs = new List<TextWriter>();

            foreach (var file in args)
                outputs.Add(new StreamWriter(file));


            int bytesRead;
                bytesRead = Console.In.ReadBlock(buffer, 0, buffer.Length);
                outputs.ForEach(o => o.Write(buffer, 0, bytesRead));
            } while (bytesRead == buffer.Length);

            outputs.ForEach(o => o.Close());

To use this you just pipe the source command into the program and provide the path of any files you want to duplicate the output to. For example:

dir | CopyToFiles files1.txt files2.txt 

Will display the results of dir as well as store the results in both files1.txt and files2.txt.

Note that there isn't much (anything!) in the way of error handling above, and supporting multiple files may not actually be required.

share|improve this answer
Hmm, download tee/cygwin for nothing or buy MSVS with my hard-earned cash for a piddly little program like that? That's a tough one :-) –  paxdiablo Apr 28 '09 at 7:08
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). –  Kris Apr 28 '09 at 7:17
Visual Studio Express is free as well, but I would still just use tee for this. –  Brian Rasmussen Apr 28 '09 at 7:39
cygwin is a pain to install. Upvote to you because this is what I was looking for. –  Samaursa Jul 24 '11 at 16:06
I don't think this outputs to the console and the files at the same time does it? –  mjaggard Nov 7 '11 at 8:50

Unfortunately there is no such thing.

Windows console applications only have a single output handle. (Well, there are two STDOUT, STDERR but it doesn't matter here) The > redirects the output normally written to the console handle to a file handle.

If you want to have some kind of multiplexing you have to use an external application which you can divert the output to. This application then can write to a file and to the console again.

share|improve this answer

This works, though it's a bit ugly:

dir >_ && type _ && type _ > a.txt

It's a little more flexible than some of the other solutions, in that it works statement-by-statement so you can use it to append as well. I use this quite a bit in batch files to log and display messages:

ECHO Print line to screen and log to file.  >_ && type _ && type _ >> logfile.txt

Yes, you could just repeat the ECHO statement (once for the screen and the second time redirecting to the logfile), but that looks just as bad and is a bit of a maintenance issue. At least this way you don't have to make changes to messages in two places.

Note that _ is just a short filename, so you'll need to make sure to delete it at the end of your batch file (if you're using a batch file).

share|improve this answer
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. –  Christopher Painter Mar 25 '11 at 19:01
Yeah, I guess this addresses a slightly different problem from what the original poster was asking. –  MTS Apr 27 '11 at 22:43
+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. –  eduncan911 Jul 19 '13 at 8:07
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. –  Nate Cook Jun 17 at 23:43
Just what I needed for my application. –  Alan Oct 7 at 17:26

mtee is a small utility which works very well for this purpose. It's free, source is open, and it Just Works.

You can find it at http://www.commandline.co.uk.

Used in a batch file to display output AND create a log file simultaneously, the syntax looks like this:

    someprocess | mtee /+ mylogfile.txt

Where /+ means to append output.

This assumes that you have copied mtee into a folder which is in the PATH, of course.

share|improve this answer
This seems to wait until the output is complete also before outputting to either the file or console. –  mellamokb Apr 17 '13 at 20:24

I agree with Brian Rasmussen, the unxutils port is the easiest way to do this. In the Batch Files section of his Scripting Pages Rob van der Woude provides a wealth of information on the use MS-DOS and CMD commands. I thought he might have a native solution to your problem and after digging around there I found TEE.BAT, which appears to be just that, an MS-DOS batch language implementation of tee. It is a pretty complex-looking batch file and my inclination would still be to use the unxutils port.

share|improve this answer
That tee.bat thing looks nice. Hope OP checks this out. –  Jay Sep 8 '09 at 17:06
Note that using TEE.BAT will output after the command has completed, just like the "dir > a.txt | type a.txt" example posted nearby. –  adzm Aug 5 '10 at 13:19

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"
share|improve this answer
This is the most close answer: it works on default install, as PS is already there on most machines and, especially, servers. –  Stoleg Feb 14 at 12:58

dir 1>a.txt 2>&1 | type a.txt

This will help to redirect both STDOUT and STDERR

share|improve this answer
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... –  djangofan Sep 21 '12 at 0:05

Here's a sample of what I've used based on one of the other answers

@echo off
set __ERROR_LOG=c:\errors.txt
REM set __IPADDRESS=x.x.x.x

REM Test a variable
if not defined __IPADDRESS (
     REM Call function with some data and terminate
     goto :EOF

REM If test happens to be successful, TEE out a message and end script.
call :TEE Script Ended Successful
goto :EOF

for /f "tokens=*" %%Z in ("%*") do (
     >  CON ECHO.%%Z
     >> "%__ERROR_LOG%" ECHO.%%Z
     goto :EOF
share|improve this answer
@echo on

set startDate=%date%
set startTime=%time%

set /a sth=%startTime:~0,2%
set /a stm=1%startTime:~3,2% - 100
set /a sts=1%startTime:~6,2% - 100

fullprocess.bat > C:\LOGS\%startDate%_%sth%.%stm%.%sts%.LOG | fullprocess.bat

This will create a log file with the current datetime and you can the console lines during the process

share|improve this answer
aren't you calling the same program twice with this? –  elcool Apr 29 '12 at 4:40

Just like unix

dir | tee a.txt

does work On windows XP, it requires mksnt installed

It displayes on the promt as well as appends to the file

share|improve this answer

If you have cygwin in your windows environment path you can use:

 dir > a.txt | tail -f a.txt
share|improve this answer

I was also looking for the same solution, after a little try, I was successfully able to achieve that in Command Prompt. Here is my solution :

@Echo off
for /f "Delims=" %%a IN (xyz.bat) do (
%%a > _ && type _ && type _ >> log.txt
@Echo on

It even captures any PAUSE command as well.

share|improve this answer

Following helps if you want something really seen on the screen - even if the batch file was redirected to a file. The device CON maybe used also if redirected to a file


ECHO first line on normal stdout. maybe redirected
ECHO second line on normal stdout again. maybe redirected
ECHO third line is to ask the user. not redirected  >CON
ECHO fourth line on normal stdout again. maybe redirected

Also see good redirection description: http://www.p-dd.com/chapter7-page14.html

share|improve this answer

How do I display and redirect output to a file. Suppose if I use dos command, dir > test.txt ,this command will redirect output to file test.txt without displaying the results. how to write a command to display the output and redirect output to a file using DOS i.e., windows command prompt, not in UNIX/LINUX.

You may find these commands in biterscripting ( http://www.biterscripting.com ) useful.

var str output
lf > $output
echo $output                            # Will show output on screen.
echo $output > "test.txt"               # Will write output to file test.txt.
system start "test.txt"                 # Will open file test.txt for viewing/editing.
share|improve this answer

This works in real time but is also kind a ugly and the performance is slow. Not well tested either:

@echo off
ECHO File called 'test.bat' > out.txt
for /f "usebackq delims=" %%I in (`%MYCOMMAND%`) do (
  ECHO %%I
  ECHO %%I >> out.txt
share|improve this answer
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 :-) –  jeb Sep 20 '12 at 18:09

An alternative is to tee stdout to stderr within your program:

in java:

System.setOut(new PrintStream(new TeeOutputStream(System.out, System.err)));

Then, in your dos batchfile: java program > log.txt

The stdout will go to the logfile and the stderr (same data) will show on the console.

share|improve this answer

I know this is a very old topic, but in previous answers there is not a full implementation of a real time Tee written in Batch. My solution below is a Batch-JScript hybrid script that use the JScript section just to get the output from the piped command, but the processing of the data is done in the Batch section. This approach have the advantage that any Batch programmer may modify this program to fit specific needs. This program also correctly process the output of CLS command produced by other Batch files, that is, it clear the screen when CLS command output is detected.

@if (@CodeSection == @Batch) @then

@echo off
setlocal EnableDelayedExpansion

rem APATee.bat: Asynchronous (real time) Tee program, Batch-JScript hybrid version
rem Antonio Perez Ayala

rem The advantage of this program is that the data management is written in Batch code,
rem so any Batch programmer may modify it to fit their own needs.
rem As an example of this feature, CLS command is correctly managed

if "%~1" equ "" (
   echo Duplicate the Stdout output of a command in the screen and a disk file
   echo anyCommand ^| APATee teeFile.txt [/A]
   echo If /A switch is given, anyCommand output is *appended* to teeFile.txt
   goto :EOF

if "%2" equ ":TeeProcess" goto TeeProcess

rem Get the output of CLS command
for /F %%a in ('cls') do set "cls=%%a"

rem If /A switch is not provided, delete the file that receives Tee output
if /I "%~2" neq "/A" if exist %1 del %1

rem Create the semaphore-signal file and start the asynchronous Tee process
echo X > Flag.out
if exist Flag.in del Flag.in
Cscript //nologo //E:JScript "%~F0" | "%~F0" %1 :TeeProcess
del Flag.out
goto :EOF

   rem Wait for "Data Available" signal
   if not exist Flag.in goto TeeProcess
   rem Read the line sent by JScript section
   set line=
   set /P line=
   rem Set "Data Read" acknowledgement
   ren Flag.in Flag.out
   rem Check for the standard "End Of piped File" mark
   if "!line!" equ ":_EOF_:" exit /B
   rem Correctly manage CLS command
   if "!line:~0,1!" equ "!cls!" (
      set "line=!line:~1!"
   rem Duplicate the line in Stdout and the Tee output file
   echo(!line!>> %1
goto TeeProcess


// JScript section

var fso = new ActiveXObject("Scripting.FileSystemObject");
// Process all lines of Stdin
while ( ! WScript.Stdin.AtEndOfStream ) {
   // Read the next line from Stdin
   var line = WScript.Stdin.ReadLine();
   // Wait for "Data Read" acknowledgement
   while ( ! fso.FileExists("Flag.out") ) {
   // Send the line to Batch section
   // Set "Data Available" signal
   fso.MoveFile("Flag.out", "Flag.in");
// Wait for last "Data Read" acknowledgement
while ( ! fso.FileExists("Flag.out") ) {
// Send the standard "End Of piped File" mark
fso.MoveFile("Flag.out", "Flag.in");
share|improve this answer

I install perl on most of my machines so an answer using perl: tee.pl

my $file = shift || "tee.dat";
open $output, ">", $file or die "unable to open $file as output: $!";
    print $_;
    print $output $_;
close $output;

dir | perl tee.pl or dir | perl tee.pl dir.bat

crude and untested.

share|improve this answer

I use a batch subroutine with a "for" statement to get the command output one line at a time and both write that line to a file and output it to the console.

@echo off
set logfile=test.log

call :ExecuteAndTee dir C:\Program Files

Exit /B 0

setlocal enabledelayedexpansion
echo Executing '%*'
  for /f "delims=" %%a in ('%* 2^>^&1') do (echo.%%a & echo.%%a>>%logfile%)
Exit /B 0
share|improve this answer

Your Answer

