Easily convert/clean Powershell UTF-16 output to UTF-8 on Mac/Linux

I work with Powershell quite often. I make healthcheck systems for apps such as NetBackup, and others. I use it for day to day administration of NetApp and vmware.

One problem I always have with it jumping between customer environments is the fact that the default text output is UTF-16. This looks like a bunch of junk when I view it in terminal on my Mac, and also it is not easily grep’able, or less/more’able.

What a more against the file looks like:

Why? Well thats how the terminal converts the unicode.

Typically in Powershell, you can issue this command, and force the Unicode output to be ascii:

The problem is, it doesn’t always work. I haven’t looked too far into the WHY of it not working, but it’s a big ol pain in the ass and when I need it to, it doesn’t.

Cleaning up this text is INCREDIBLY easy! 1 command. That’s it. Iconv to the rescue. You can use portage to install it. “sudo port install iconv”

Make a file in your users ~/bin/ directory called catutf16

Then change the perms to make it executable

After that, you can simply process ALL files or just one file at a time to output it to a new file in the right text encoding.

Want to see one file?

Want to convert ALL files in a directory?

It’s just that easy. UTF-16 to UTF8. You can change any of the charactersets as your destination.

Be Sociable, Share!

, ,