Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 18 down vote accepted

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.

share|improve this answer
4  
Still hard-coded in PowerShell 5 on Windows 10, unfortunately: CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExt‌​ent, "Encoding", "-Encoding:", PositionUtilities.EmptyExtent, "Unicode", false, false); – Artfunkel Sep 17 '15 at 9:00

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
share|improve this answer
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .