Powershell gotchas: redirect to file encodes in Unicode

April 20, 2012

...

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?

Profile picture

Written by Vidar Kongsli who is a software professional living in Oslo, Norway. Works as a consultant, system architect and developer at Bredvid. You should follow him on Twitter