Displaying Windows command prompt output and redirecting it to a file
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.
Admin
Updated on July 08, 2022Comments
-
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 calledtest.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 about 15 yearsYou 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 almost 15 yearsLots of Unix utilities are also ported by GnuWin32 project, see gnuwin32.sourceforge.net.
-
Leigh Riffel almost 15 yearsThis satisfies the answer, but outputs the data after the dir command has completed rather than as the data is produced.
-
Jay over 14 yearsHome-made tee implementation? And have to learn C#? Use tee.. really.
-
Jay over 14 yearsThat tee.bat thing looks nice. Hope OP checks this out.
-
quack quixote over 14 yearsUnxUtils was last updated in 2003; GnuWin32 is a bit more up-to-date.
-
adzm almost 14 yearsNote 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 about 13 yearsThat'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 about 13 yearsYeah, I guess this addresses a slightly different problem from what the original poster was asking.
-
Samaursa almost 13 yearscygwin is a pain to install. Upvote to you because this is what I was looking for.
-
mjaggard over 12 yearsI don't think this outputs to the console and the files at the same time does it?
-
markmnl over 12 yearswhy use this when there is native support in windows as per the answer below??
-
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 about 12 yearsaren't you calling the same program twice with this?
-
jeb over 11 yearsNo, 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 over 11 yearsThis 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 over 11 yearsWhy 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 over 11 yearsWhen down voting please leave a comment. Thanks.
-
mellamokb about 11 yearsThis seems to wait until the output is complete also before outputting to either the file or console.
-
Vitim.us about 11 yearsit will work, but you will get stuck if the command wait a input from stdin.
-
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 over 10 yearsThis is the most close answer: it works on default install, as PS is already there on most machines and, especially, servers.
-
Nigel Touch over 10 yearsIf you, like me, struggled to find GnuWin32's
tee
package, you'll find it in gnuwin32.sourceforge.net/packages/coreutils.htm. -
ADTC about 10 yearsYou should use
&
instead of&&
if you want to ensure thetype
command is executed even ifdir
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 oftype
(which would be 0). -
Nate Cook almost 10 yearsThis 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 over 9 yearsJust what I needed for my application.
-
Mr. Llama about 9 yearsIf you're going to use Cygwin, it comes with tee.
-
John Demetriou over 8 yearscare to explain what's going on here?
-
Wes Larson over 8 yearsI like this simple answer! I found that an
&
instead of a|
is needed for the output of some commands, likeping
or7z.exe
-
Sopalajo de Arrierez almost 8 yearsNot 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 almost 8 yearsI 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 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 over 7 yearsThis is the best answer! Simple and works "out of the box"
-
Lii over 7 yearsThis 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 over 7 yearsThat's the only solution working for me, not having Powershell > 4.0. Thumbs up!
-
Pavel P about 7 yearsAll 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. With2>&1
inside cmd line any output to stderr causes errors from powershell. -
Ionut about 7 yearsIt works, but the file will not contain all the output
-
grenix about 7 yearsLIKE IT :) anyhow {powershell "ping -n 10 localhost | tee test.txt"} would demonstrate better whats going on on the tee party
-
Jesse Chisholm almost 7 years@lii re:
handle output to stderr
- Add the2>&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 almost 7 yearsnote that
dir
in powershell is an alias toGet-ChildItem
which is not like cmd's internaldir
command. You needpowershell "cmd /c dir | tee test.txt"
if you want the cmd internal version -
phuclv almost 7 years
tee
on *nix is also a separate application, not some redirection on the shell -
Zhe Hu almost 7 yearsto see all the output, don't forget to redirect STDERR to STDOUT with
2>&1
, for examplenode 2>&1 | tee debug_log.txt
-
Bernd over 6 yearsCould be a reference to the PowerShell Tee-Object cmdlet - technet.microsoft.com/en-us/library/ee177014.aspx
-
u8it over 6 yearsI 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 over 6 yearsyeah but i got ugly whitespace, any direction?
-
JustAMartin over 6 yearsAlmost, but it does not display simultaneously - if you have a long running task, you will get no results for long time.
-
dehinrsu over 6 yearsBut you lose the text color on console output.
-
Ben Personick about 6 yearsI 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 over 5 yearsmjaggard: god point. It could easily be modified to do it though by adding an additional output that WAS the console.out stream.
-
R.F about 5 yearsIs is very useful command, we we want to have everything, we need 2>&1
-
Edi almost 5 yearsConsider 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 about 4 yearsAny 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 about 4 yearsI figured it out. You can do it using
file.exe 2>&1|tee.bat
as found here: superuser.com/questions/452763/… -
zett42 about 4 yearsWith
2>&1
any stderr output will cause a "NativeCommandError" to be displayed, intermixed with the actual output. To prevent that usepowershell ".\something.exe 2>&1 | foreach {\"$_\"} | tee test.txt"
. This converts any error records to plain text before sending it totee
. -
Andry almost 4 yearsMore details is here: stackoverflow.com/questions/796476/…
-
user2956477 almost 4 yearsunixutils 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 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 over 3 yearssee stackoverflow.com/questions/52970939/… so it's not all red
-
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 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 over 3 yearsCygwin 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 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 over 2 yearsThis is not writing to a file in windows 10
-
Aleksey F. over 2 yearsThis should be the accepted answer.
wtee
works onWin 2003 Srv
at least, starts much faster thanpowersell
and can be easily integrated into the building process. -
Nikolai Ehrhardt over 2 yearsdid 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 about 2 yearsBeware of issues: github.com/ritchielawrence/mtee/issues/4
-
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 about 2 yearsYou 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.exe --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 likec:\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