powershellencodingutf-8powershell-4.0tee

tee with utf-8 encoding


I'm trying to tee a server's output to both the console and a file in Powershell 4. The file is ending up with a UTF-16 encoding, which is incompatible with some other tools I'm using. According to help tee -full:

Tee-Object uses Unicode enocding when it writes to files.
...
To specify the encoding, use the Out-File cmdlet

So tee doesn't support changing encoding, and the help for both tee and Out-File don't show any examples of splitting a stream and encoding it with UTF-8.

Is there a simple way in Powershell 4 to tee (or otherwise split a stream) to a file with UTF-8 encoding?


Solution

  • One option is to use Add-Content or Set-Content instead of Out-File.

    The *-Content cmdlets use ASCII encoding by default, and have a -Passthru switch so you can write to the file, and then have the input pass through to the console:

    Get-Childitem -Name | Set-Content file.txt -Passthru