Weird Spaces Between Characters in PowerShell Output

Have you ever redirected output from PowerShell into a file, only to find that there are spaces between every character?  Have you ever redirected output from PowerShell into a config file that the program just absolutely refuses to read in, even through it looks exactly the same as a known-good config file?  If so, you probably have encoding issues.  The good news?  They're really easy to fix, once you know what's going on.

So, by default that nice ">" output redirect uses Unicode.  Most of the time, that works just fine... but sometimes, especially if you're working with legacy software, it's going to cause you problems.  Here's a TechNet article on the issue.  Let's take a quick look at some examples - run this command in PowerShell to generate an example file:

echo "Hello World" > test.txt

Then open up test.txt in Notepad, hit "Save As" and look at the bottom of the window.  There's an "Encoding" section that'll say "Unicode" (sorry, this was the easiest way that I was able to find to determine what encoding is on an existing file, although that TechNet article has a nice looking PowerShell script to do exactly that).

As mentioned, Unicode is great, except for when it's not.  What do you do if you need to write a config file for an old DOS program?  Well, you're probably going to need to use ASCII encoding.  How about for a Linux system?  You might find that UTF8 is the way to go.  So, how do you do that?  Really easily:

echo "Hello World" | set-content -encoding UTF8 test.txt

Go ahead and check that file out in Notepad - you'll see that it now reads as being UTF8.  The Set-Content command is great, for it allows you to explicitly set the encoding on that content.  If you take a look at get-help set-content you'll see that it supports many common formats, including Big Endian Unicode, UTF32, ASCII, and many others.


Popular posts from this blog

Deleting Orphaned (AKA Zombie) VMDK Files

Clone a Standard vSwitch from one ESXi Host to Another

vCenter Server Appliance Crash due to Full /Storage/SEAT Partition