9

I want to see the output of the command in the console and save it in a file so I have these two options:

When I use command | tee output.txt somehow it generates no output file at all but it works as usual in the console.

When I use command 2>&1 | tee output.txt it generates a fine output file but the text in the console appears in red. enter image description here

Is there any way to either fix the first option or let the text appear as usual in the second one? I am using Windows PowerShell (Windows 10) and the Programm I am using this for is liquibase 3.5.5. just for the case that this is important.

5
  • maybe command | out-string | tee output.txt ? Commented Oct 24, 2018 at 14:05
  • @JacobColvin when I do this it generates an empty txt file Commented Oct 24, 2018 at 14:19
  • Your executable is writing to the error stream for some reason. You still need 2>&1 to capture its output. Commented Oct 24, 2018 at 14:49
  • @TheIncorrigible1 so the | tee parameter generates no output file because it is an error stream? Commented Oct 24, 2018 at 14:57
  • Correct. By default, it's looking at the success (&1) stream. You'd need to do & command 2>&1 | tee ... Commented Oct 24, 2018 at 14:59

2 Answers 2

9

In PowerShell, redirecting stderr lines from an external program to PowerShell's success stream via 2>&1 wraps those lines in [System.Management.Automation.ErrorRecord] instances in case they are captured for further processing.

In Windows PowerShell, these captured instances render like PowerShell errors, which is why you're seeing the red output (by contrast, without the redirection, the stderr lines would be passed through to the console, without coloring).

A simple workaround is to convert these objects to strings explicitly, which outputs the original lines (PSv3+ syntax; built-in alias % for ForEach-Object used for brevity):

... 2>&1 | % ToString | Tee-Object output.txt

Note: This workaround is no longer necessary in the install-on-demand, cross-platform PowerShell (Core) 7+ edition, where even captured stderr lines now consistently render just as strings.

1
  • 2
    Redirecting all streams (not just Error and Output/Success but also Warning, Verbose and Debug) is possible with *>&1.
    – eel ghEEz
    Commented May 18, 2022 at 16:19
1

If you are in the Command Prompt, then you can leverage PowerShell (from the Command Prompt) and use Tee-Object to more or less do a tee like in a POSIX shell.

C:\> powershell some_command ^| tee-object -FilePath some_outputfile

(The ^ escapes the pipe so that the pipe applies to PowerShell and not the Command Prompt)

PowerShell and Tee-Object come with Windows as standard so nothing to install!

1
  • 6
    I assume you mean cmd.exe aka the Command Prompt, the command shell of modern Windows versions, not the long-obsolete DOS and its command.com counterpart. Given that this question isn't about calling Tee-Object from outside PowerShell, your answer amounts to a distraction.
    – mklement0
    Commented May 18, 2022 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.