Windows – How to both pipe and display output in Windows’ command line

command linepipepowershellteewindows

I have a process I need to run within a batch file. This process produces some output. I need to both display this output to the screen and send (pipe) it to another program.

The bash method uses tee:

echo 'ee' | tee /dev/tty | foo

Is there an equivalent for Windows? I am happy to use PowerShell if necessary.

There are tee ports for Windows, but there does not appear to be an equivalent for /dev/tty, which complicates matters.


The specific use-case here: I have a program (launch4j) that I need to run, displaying output to the user. At the same time, I need to be able to detect success or failure in the script. Unfortunately, this program does not set an exit code, and I cannot force it to do so. My current workaround involves piping to find, to search the output (launch4j config.xml | find "Successfully created") – however, that swallows the output I need to display. Therefore, I need some way to both display to the screen and send the ouput to a command – and this command should be able to set ERRORLEVEL (it cannot run asynchronously). This will be used in a build script, which could be run on many different machines.

For this particular case, something lightweight is required – I cannot install additional frameworks or interpreters (e.g. perl as suggested in this answer). Also, any commercial programs must have a licence that allows redistribution.

Best Answer

You could try compiling this code and using it like: echo something | mytee | foo.
I don't know if it will work, since I don't know how Windows deals with stderr/stdout, but it might work.

#include <stdio.h>
int main()
{
    int c;
    while((c = fgetc(stdin)) != EOF)
    {
        printf("%c", c);
        fprintf(stderr, "%c", c);
    }
    return 0;
}
Related Question