diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 8551c81bfbf..42167e00f55 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -4410,7 +4410,7 @@ .ForwardHelpCategory Cmdlet // Variable which controls the encoding for piping data to a NativeCommand new SessionStateVariableEntry( SpecialVariables.OutputEncoding, - System.Text.Encoding.ASCII, + Utils.utf8NoBom, RunspaceInit.OutputEncodingDescription, ScopedItemOptions.None, new ArgumentTypeConverterAttribute(typeof(System.Text.Encoding)) diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index cee3678d77d..c51e730495f 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -1798,7 +1798,7 @@ internal void Start(Process process, NativeCommandIOFormat inputFormat) //from the current scope so a script or function can use a different encoding //than global value. Encoding pipeEncoding = _command.Context.GetVariableValue(SpecialVariables.OutputEncodingVarPath) as System.Text.Encoding ?? - Encoding.ASCII; + Utils.utf8NoBom; _streamWriter = new StreamWriter(process.StandardInput.BaseStream, pipeEncoding); _streamWriter.AutoFlush = true; diff --git a/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 b/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 index 987c5416d94..f3823b93563 100644 --- a/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 +++ b/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 @@ -1,3 +1,10 @@ +Describe 'Native pipeline should have proper encoding' -tags 'CI' { + It '$OutputEncoding should be set to UTF8 without BOM' { + $OutputEncoding.BodyName | Should Be "utf-8" + $OutputEncoding.GetPreamble().Length | Should Be 0 + } +} + Describe 'native commands with pipeline' -tags 'Feature' { BeforeAll {