35

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
  • 1,435
  • 3
  • 14
  • 19
  • 1
    thanks for the tip for an alternative, doing this from the terminal can be quicker than a text editor sometimes. – Todd Partridge Jun 05 '17 at 15:24
  • 1
    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 May 22 '18 at 11:44

3 Answers3

23

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
  • 348
  • 2
  • 5
  • 5
    Still hard-coded in PowerShell 5 on Windows 10, unfortunately: `CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, "Encoding", "-Encoding:", PositionUtilities.EmptyExtent, "Unicode", false, false);` – Artfunkel Sep 17 '15 at 09:00
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
Glorfindel
  • 4,089
  • 8
  • 24
  • 37
OldWolf
  • 2,423
  • 15
  • 15
  • 2
    I 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 Nov 16 '12 at 15:49
1

You need to change the default encoding of redirection operators (> and >>). The Out-File cmdlet is used behind the scenes when you use these operators.

$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'

See Powershell Docs for details.

Yura Loginov
  • 111
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 10 '23 at 11:53
  • 1
    Although as noted no longer an issue in Powershell 6+, this should be the accepted answer – caduceus May 25 '23 at 12:34