You will need to use -Variable , and then write it to a file in a separate step.
$data = $null Get-Process | Tee-Object -Variable data $data | Out-File -Path $path -Encoding Utf8
At first glance, it seems that it is easier to avoid tee altogether and simply capture the output in a variable, and then write it to the screen and to a file.
But because of how the pipeline works, this method allows a long pipeline to display data on the screen as it moves. Unfortunately, the same cannot be said for a file that will not be written until it is.
Doing both
An alternative is to flip your own tee so to speak:
[String]::Empty | Out-File -Path $path # initialize the file since we're appending later Get-Process | ForEach-Object { $_ | Out-File $path -Append -Encoding Utf $_ }
This will be written to the file and returned to the pipeline, and this will happen when it passes. This is probably pretty slow.
source share