To complement zett42’s helpful answer:
If you’re running PowerShell (Core) 7+, you can pass the file path that represents the terminal (console) to the (positionally implied) -FilePath
parameter (in Windows PowerShell, this causes an error, unfortunately – see bottom section):
# PowerShell 7+ only
# Windows
Get-Content data.txt | Tee-Object \\.\CON | data_processor.exe
# Unix-like platforms (Linux, macOS)
Get-Content data.txt | Tee-Object /dev/tty | data_processor.exe
# Platform-agnostic
Get-Content data.txt | Tee-Object ($IsWindows ? '\\.\CON': '/dev/tty') | data_processor.exe
This passes all data through while also printing it to the terminal (console), richly formatted, as usual, as it becomes available – unlike with the Tee-Object -Variable
approach, which requires collecting all output in memory first (which is a concern both in terms of output timing and memory consumption).
Windows PowerShell solution: Custom proxy (wrapper) function Tee-Host
wraps Out-Host
while also passing its input through; use it instead of Tee-Object
:
function Tee-Host {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)] $InputObject
)
begin
{
$scriptCmd = { Out-Host }
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
}
process
{
# Pass to Out-Host, and therefore to the host (terminal)
$steppablePipeline.Process($InputObject)
# Pass through (to the success stream)
$InputObject
}
end
{
$steppablePipeline.End()
}
}
-
In effect,
Tee-Host
behaves likeTee-Object \\.\CON
/Tee-Object /dev/tty
in PowerShell 7+, whereTee-Host
works too. -
Even in PowerShell 7+
Tee-Host
may be preferable, because it uses colored output unconditionally, whereas the coloring behavior ofTee-Object \\.\CON
/Tee-Object /dev/tty
depends on the value of$PSStyle.OutputRendering
It is the use of a proxy function with a steppable pipeline wrapping Out-Host
that ensures that the to-host formatted output looks the same as when the input is directly sent to the host.