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 Partridge– Todd Partridge2017-06-05 15:24:06 +00:00Commented 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/4878kumarharsh– kumarharsh2018-05-22 11:44:24 +00:00Commented 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);Artfunkel– Artfunkel2015-09-17 09:00:59 +00:00Commented 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
$OutputEncodingis 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$OutputEncodingcontrols.Benjamin Pollack– Benjamin Pollack2012-11-16 15:49:43 +00:00Commented 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.2023-04-10 11:53:54 +00:00Commented Apr 10, 2023 at 11:53
- 1Although as noted no longer an issue in Powershell 6+, this should be the accepted answercaduceus– caduceus2023-05-25 12:34:03 +00:00Commented May 25, 2023 at 12:34