Skip to content

Free, open-source Windows optimization tool for performance, privacy, and simplicity.

fix: add UTF-8 encoding to PowerShell

fatDuck5573b2491ac5
diff --git a/optimizerDuck.Test/Services/OptimizationServices/ShellServiceTests.cs b/optimizerDuck.Test/Services/OptimizationServices/ShellServiceTests.cs
index dc4a110..0843990 100644
--- a/optimizerDuck.Test/Services/OptimizationServices/ShellServiceTests.cs
+++ b/optimizerDuck.Test/Services/OptimizationServices/ShellServiceTests.cs
@@ -1,4 +1,6 @@
+using System.Text;
using optimizerDuck.Services.OptimizationServices;
+using Xunit;
namespace optimizerDuck.Test.Services.OptimizationServices;
@@ -27,4 +29,17 @@ public class ShellServiceTests
Assert.Equal(0, result.ExitCode);
}
+
+ [Theory]
+ [InlineData("Tiếng Việt có dấu")]
+ [InlineData("Zażółć gęślą jaźń")]
+ [InlineData("Español – información")]
+ [InlineData("Français – élève")]
+ [InlineData("Deutsch – äußern")]
+ public void PowerShell_MultiLanguageUnicode_ReturnsCorrectString(string text)
+ {
+ var result = ShellService.PowerShell($"Write-Output '{text}'");
+
+ Assert.Contains(text, result.Stdout);
+ }
}
\ No newline at end of file
diff --git a/optimizerDuck/Services/OptimizationServices/ShellService.cs b/optimizerDuck/Services/OptimizationServices/ShellService.cs
index 4863c6d..1661612 100644
--- a/optimizerDuck/Services/OptimizationServices/ShellService.cs
+++ b/optimizerDuck/Services/OptimizationServices/ShellService.cs
@@ -62,17 +62,25 @@ public static class ShellService
{
policy ??= ShellPolicy.Default;
- var commandForUser = arguments.Contains("-EncodedCommand", StringComparison.OrdinalIgnoreCase)
- ? command.DecodeBase64().Replace("$ProgressPreference='SilentlyContinue'; ", "")
- : command;
+ var commandForUser = (arguments.Contains("-EncodedCommand", StringComparison.OrdinalIgnoreCase)
+ ? command.DecodeBase64()
+ : command)
+ .Replace("$ProgressPreference='SilentlyContinue'; ", "")
+ .Replace("[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; ", "")
+ .Replace("$OutputEncoding = [System.Console]::OutputEncoding = [System.Console]::InputEncoding = [System.Text.Encoding]::UTF8; ", "")
+ .Replace("chcp 65001 > nul & ", "");
var fullCommandForUser =
$"{fileName} {arguments.Replace("-EncodedCommand", "-Command", StringComparison.OrdinalIgnoreCase)} {commandForUser}";
+ var processArgs = fileName.Equals("cmd.exe", StringComparison.OrdinalIgnoreCase) && !arguments.Contains("chcp 65001") // try best effort to ensure UTF-8 codepage for cmd, but better use PowerShell if possible
+ ? $"{arguments} chcp 65001 > nul & {command}"
+ : $"{arguments} {command}";
+
var psi = new ProcessStartInfo
{
FileName = fileName,
- Arguments = $"{arguments} {command}",
+ Arguments = processArgs,
RedirectStandardOutput = true,
RedirectStandardError = true,
StandardOutputEncoding = Encoding.UTF8,
@@ -206,9 +214,13 @@ public static class ShellService
{
policy ??= ShellPolicy.Default;
- var commandForUser = arguments.Contains("-EncodedCommand", StringComparison.OrdinalIgnoreCase)
- ? command.DecodeBase64().Replace("$ProgressPreference='SilentlyContinue'; ", "")
- : command;
+ var commandForUser = (arguments.Contains("-EncodedCommand", StringComparison.OrdinalIgnoreCase)
+ ? command.DecodeBase64()
+ : command)
+ .Replace("$ProgressPreference='SilentlyContinue'; ", "")
+ .Replace("[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; ", "")
+ .Replace("$OutputEncoding = [System.Console]::OutputEncoding = [System.Console]::InputEncoding = [System.Text.Encoding]::UTF8; ", "")
+ .Replace("chcp 65001 > nul & ", "");
var fullCommandForUser =
$"{fileName} {arguments.Replace("-EncodedCommand", "-Command", StringComparison.OrdinalIgnoreCase)} {commandForUser}";
@@ -343,7 +355,6 @@ public static class ShellService
}
}
-
#region Command Prompt methods
public static ShellResult CMD(string command, ShellRevertStep? revertStep = null, ShellPolicy? policy = null)
@@ -367,7 +378,6 @@ public static class ShellService
ct);
}
-
public static ShellResult CMD(string command, string revertCommand, ShellPolicy? policy = null)
{
return CMD(command, new ShellRevertStep
@@ -392,7 +402,11 @@ public static class ShellService
public static ShellResult PowerShell(string command, ShellRevertStep? revertStep = null, ShellPolicy? policy = null)
{
- command = "$ProgressPreference='SilentlyContinue'; " + command;
+ command =
+ "$ProgressPreference='SilentlyContinue'; " +
+ "[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; " +
+ "$OutputEncoding = [System.Console]::OutputEncoding = [System.Console]::InputEncoding = [System.Text.Encoding]::UTF8; " +
+ command;
return Run("powershell.exe",
"-NonInteractive -NoLogo -NoProfile -ExecutionPolicy Bypass -EncodedCommand",
command.EncodeBase64(),
@@ -407,7 +421,11 @@ public static class ShellService
ShellPolicy? policy = null,
CancellationToken ct = default)
{
- command = "$ProgressPreference='SilentlyContinue'; " + command;
+ command =
+ "$ProgressPreference='SilentlyContinue'; " +
+ "[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; " +
+ "$OutputEncoding = [System.Console]::OutputEncoding = [System.Console]::InputEncoding = [System.Text.Encoding]::UTF8; " +
+ command;
return RunAsync("powershell.exe",
"-NonInteractive -NoLogo -NoProfile -ExecutionPolicy Bypass -EncodedCommand",
command.EncodeBase64(),