[ACCEPTED]-Using redirection within the script produces a unicode output. How to emit single-byte ASCII text?-powershell

Accepted answer
Score: 21

You could change the $OutputEncoding variable before writing 3 to the file. The other option is not to 2 use the > operator, but instead pipe directly 1 to Out-File and use the -Encoding parameter.

Score: 13

The > redirection operator is a "shortcut" to 2 Out-File. Out-File's default encoding is Unicode but you 1 can change it to ASCII, so pipe to Out-File instead:

Get-Content -Encoding ASCII $projFile.FullName |
    % { $_ -replace '<FooterText>(.+)</FooterText>', $newFooter } |
    Out-File $tmpfile -Encoding ASCII
Score: 7

| sc filename does the trick (sc being an alias for Set-Content)

for 2 >> filename use | ac filename does the trick (ac being an alias 1 for Add-Content)

Score: 4

I found I had to use the following:

write-output "First line" | out-file -encoding ascii OutputFileName
write-output "Next line" | out-file -encoding ascii -append OutputFileName
....

Changing 1 the output encoding using:

$OutputEncoding = New-Object -typename System.Text.ASCIIEncoding

did not work

Score: 3

You can set the default encoding of out-file 4 to be ascii:

$PSDefaultParameterValues=@{'out-file:encoding'='ascii'}

Then something like this will 3 result in an ascii file:

echo hi > out

In powershell 6 2 and 7, the default encoding of out-file 1 was changed to utf8 no bom.

Score: 2

Just a little example using streams, although 4 I realize this wasn't the original question.

C:\temp\ConfirmWrapper.ps1 -Force -Verbose 4>&1 6>&1 | Out-File -Encoding default -FilePath C:\temp\confirmLog.txt -Append

Will 3 output the information(6) and verbose(4) streams 2 to the output(1) stream and redirect all 1 that to the out-file with ANSI(default) encoding.

More Related questions