Get output in UTF8

Андрей Михалевский 3,451 Reputation points
2021-07-05T14:47:00.513+00:00

Hi. I have code powershell:

$DisableACMonitorTimeOut = Execute-Command -commandTitle "test" -commandPath "C:\Program Files\MyProgram\file.exe" -commandArguments "-help"

$DisableACMonitorTimeOut.stdout

Function Execute-Command ($commandTitle, $commandPath, $commandArguments)
{
    Try {
        $pinfo = New-Object System.Diagnostics.ProcessStartInfo
        $pinfo.FileName = $commandPath
        $pinfo.RedirectStandardError = $true
        $pinfo.RedirectStandardOutput = $true
        $pinfo.UseShellExecute = $false
        $pinfo.WindowStyle = 'Hidden'
        $pinfo.CreateNoWindow = $True
        $pinfo.Arguments = $commandArguments
        $p = New-Object System.Diagnostics.Process
        $p.StartInfo = $pinfo
        $p.Start() | Out-Null
        $stdout = $p.StandardOutput.ReadToEnd()
        $stderr = $p.StandardError.ReadToEnd()
        $p.WaitForExit()
        $p | Add-Member "commandTitle" $commandTitle
        $p | Add-Member "stdout" $stdout
        $p | Add-Member "stderr" $stderr
    }
    Catch {
    }
    $p
}

How i can got output in UTF8 ?

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

12 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2021-07-09T14:17:33.287+00:00

    I found this... add one of these lines and see if you get different results.

    $pinfo.StandardOutputEncoding =  [System.Text.Encoding]::UTF8 
    

    or

     $pinfo.StandardOutputEncoding =  [System.Text.Encoding]::Unicode 
    
    0 comments No comments

  2. Dyakov Alexander 1 Reputation point
    2022-04-07T08:08:11.933+00:00

    UTF8 not work for me, instead, I use next:

    $pinfo.StandardOutputEncoding = [System.Text.Encoding]::GetEncoding(866)
    

    https://stackoverflow.com/questions/16803748/how-to-decode-cmd-output-correctly/26015551#26015551

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.