Default PowerShell to emitting UTF-8 instead of UTF-16?

31

7

By default, PowerShell in Windows seems to be outputting UTF-16 (e.g., if I do a simple echo hello > hi.txt, then hi.txt ends up in UTF-16). I know that I can force this to my desired text encoding by instead doing echo hello | out-file -encoding utf8 hi.txt, but what I'd like is for that to just be the default when I use the redirection operator. Is there any way to achieve this?

Benjamin Pollack

Posted 2011-08-24T15:35:41.883

Reputation: 1 385

1thanks for the tip for an alternative, doing this from the terminal can be quicker than a text editor sometimes. – Todd Partridge – 2017-06-05T15:24:06.727

In Powershell 6.0, this has been rendered unnecessary - Powershell now defaults to UTF-8 without encoding for redirection. See https://github.com/PowerShell/PowerShell/issues/4878

– kumarharsh – 2018-05-22T11:44:24.487

Answers

22

Using a .NET decompiler on the System.Management.Automation assembly (a.k.a. the "Microsoft Windows PowerShell Engine Core Assembly") reveals this code fragment:

// class: System.Management.Automation.RedirectionNode
private PipelineProcessor BuildRedirectionPipeline(string path, ExecutionContext context)
{
    CommandProcessorBase commandProcessorBase = context.CreateCommand("out-file");
    commandProcessorBase.AddParameter("-encoding", "unicode");
    if (this.Appending)
    {
        commandProcessorBase.AddParameter("-append", true);
    }
    commandProcessorBase.AddParameter("-filepath", path);
    ...

So, it looks pretty hardcoded to me.

FYI, this was on Windows 7 Enterprise x64 system with PowerShell 2.0 installed.

Tinister

Posted 2011-08-24T15:35:41.883

Reputation: 338

5Still hard-coded in PowerShell 5 on Windows 10, unfortunately: CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, "Encoding", "-Encoding:", PositionUtilities.EmptyExtent, "Unicode", false, false); – Artfunkel – 2015-09-17T09:00:59.950

3

Not sure if this will do exactly what you're looking for, but you can try setting the environment variable as mentioned here

$OutputEncoding = New-Object -typename System.Text.UTF8Encoding

OldWolf

Posted 2011-08-24T15:35:41.883

Reputation: 2 293

2I don't think $OutputEncoding is quite what I need; that's set to ASCII in PowerShell, and affects how things are displayed. What I want to do is to change the format of text that's saved in a file, which (AFAICT) is different than what $OutputEncoding controls. – Benjamin Pollack – 2012-11-16T15:49:43.760