Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I am using Sandcastle Helpfile Builder to produce a helpfile (.chm). The project is a .shfbproj file, which is XML format, works with msbuild.

I want to automatically update the Footer text that appears in the generated .chm file. I use this snippet:

$newFooter = "<FooterText>MyProduct v1.2.3.4</FooterText>";  get-content  -Encoding ASCII $projFile.FullName |      %{$_ -replace '<FooterText>(.+)</FooterText>', $newFooter } > $TmpFile  move-item $TmpFile $projFile.FullName -force 

The output directed to the $TmpFile is always a multi-byte string. But I don't want that. How do I set the encoding of the output to ASCII?

like image 991
Cheeso Avatar asked Nov 10 '09 11:11

Cheeso


People also ask

What is redirection in PowerShell?

Windows PowerShell has two redirection operators, > and >>. A redirection operator redirects the output of a command (or pipeline) to a specified location. The > operator creates a new file and redirects text to it or, if the file exists, it overwrites the existing content.


2 Answers

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

like image 163
tomasr Avatar answered Sep 28 '22 03:09

tomasr


The > redirection operator is a "shortcut" to Out-File. Out-File's default encoding is Unicode but you 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 
like image 23
Shay Levy Avatar answered Sep 28 '22 02:09

Shay Levy