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?
-
1thanks for the tip for an alternative, doing this from the terminal can be quicker than a text editor sometimes.– Todd PartridgeCommented Jun 5, 2017 at 15:24
-
1In Powershell 6.0, this has been rendered unnecessary - Powershell now defaults to UTF-8 without encoding for redirection. See github.com/PowerShell/PowerShell/issues/4878– kumarharshCommented May 22, 2018 at 11:44
3 Answers
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.
-
5Still hard-coded in PowerShell 5 on Windows 10, unfortunately:
CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, "Encoding", "-Encoding:", PositionUtilities.EmptyExtent, "Unicode", false, false);
Commented Sep 17, 2015 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
-
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. Commented Nov 16, 2012 at 15:49
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.
-
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.– Community BotCommented Apr 10, 2023 at 11:53
-
1Although as noted no longer an issue in Powershell 6+, this should be the accepted answer– caduceusCommented May 25, 2023 at 12:34