Powershell gotchas: redirect to file encodes in Unicode

The other day, I wrote a Powershell script that would manipulate a Windows command prompt file. After some troubleshooting (note to self: RTFM) I found Powershell file redirection encodes in Unicode, more specifically UCS-2 Little Endian. The problem in this case is that cmd.exe does not understand Unicode. So, the follwing yields a not runnable file:

@'
@echo off
echo Wi nøt trei a høliday in Sweden this ÿer?
'@ > '.\Opening titles (unicode).cmd'
& '.\Opening titles (unicode).cmd'
Output: '■@' is not recognized as an internal or external command,
operable program or batch file.
The trick is to use the Out-File Cmdlet instead and explicitly set the encoding. So, the following yields runnable code:
@'
@echo off
echo Wi nøt trei a høliday in Sweden this ÿer?
'@ | Out-file '.\Opening titles (Ascii).cmd' -Encoding ASCII
& '.\Opening titles (Ascii).cmd'
Output: Wi n?t trei a h?liday in Sweden this ?er?
In this case, it executes. However, the non-ASCII characters are not properly displayed. To achieve that as well, I used the following:
@'
@echo off
echo Wi nøt trei a høliday in Sweden this ÿer?
'@ | Out-file '.\Opening titles (OEM).cmd' -Encoding OEM
& '.\Opening titles (OEM).cmd'
Output: Wi nøt trei a høliday in Sweden this ÿer?

Share
  • Aaron

    So cmd > out 2> err with out and err can’t be easier than cmd > out 2> err; cat out | out-file out2 utf8; rm out; mv out2 out; cat err | out-file err2 utf8; rm err; mv err2 err; ? Really a tragedy

  • http://www.kongsli.net/nblog/ Vidar Kongsli

    Can’t think of any better solution… :-(