I use this command to see output both in the console and a file:
powershell -command "my_command_1 | tee ('logs\{0}.log' -f (Get-Date -format 'yyyy.MM.dd-HH.mm'))"
powershell -command "my_command_2 | tee ('logs\{0}.log' -f (Get-Date -format 'yyyy.MM.dd-HH.mm'))"
# etc
It works not as good as I expected and I have some questions:
- How do I redirect stderr also to a file?
-
The output works very strange. For some commands there is a huge delay between text is printed and console/file is updated. For some other commands output seems updated when text is printed (I run commands without tee and I know what it should print). This delay makes this tee almost useless – what if some critical error will be printed so I need to stop the command, but I won't see anything until it's too late?
For some commands output is printed only after the full command is done.
- Furthermore, even if the command asks user input, the console/file output is empty! For that command I know what it expects and blindly print needed text and it worked, but for others – with no output I'll wait something to happen infinitely while the command will wait for my input!
Are there solutions for these issues? If not, this tee thing in PowerShell is completely useless.
Best Answer
My-Command 2>&1 | Tee-Object 'myfile.log'
. SeeGet-Help about_Redirection
.Get-Help about_Try_Catch_Finally
. Is the command you're running an external program or a script?Tee
might handle partial strings fine, but other cmdlets likeForEach-Object
orSelect-Object
certainly would not. Note thatGet-Content
has a special switch-ReadCount
which somewhat overrides this behavior, and it will seriously mess with aSelect-Object -Skip/-First/-Last/-Unique
command further down the pipe.It very well may be that external programs you're running aren't going to obey the conventions that PowerShell is expecting.
Tee
, for example, is properly calledTee-Object
, which should tell you the kinds of things it's good at working with. In that case, you may be further along the line to gettee.exe
from GNU Win32 Utils or MSYS which are designed for immediately forwarding content.