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-05T16:14:10.667+00:00

    It might depend on what file.exe is and the output it produces.

    This example seems to work for me.

    If you question is really "how do I output the contents of a variable into a file as utf8", just look at how I create the utfString.txt file.

     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  
     }  
      
     $utfString = '蓝屏日志 请问怎么处理呢'  
     $utfString | out-file -FilePath c:\temp\utfString.txt -Encoding utf8       # create a file with utf8 contents   
      
     $DisableACMonitorTimeOut = Execute-Command -commandTitle "test" -commandPath "C:\windows\system32\cmd.exe" -commandArguments "/c type c:\temp\utfString.txt"  
          
     "Here is stdout: {0}" -f $DisableACMonitorTimeOut.stdout  
      
    

    111914-capture.jpg

    0 comments No comments

  2. Андрей Михалевский 3,451 Reputation points
    2021-07-06T06:43:54.173+00:00

    112043-%D1%81%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA.png

    I got like this. Text in Russian language. I need got in UTF8, wihtout import\export in file.

    0 comments No comments

  3. MotoX80 36,291 Reputation points
    2021-07-06T13:01:29.6+00:00

    If you change my example to include Russian words, does it work? I changed this statement and it displays ok.

     $utfString = '蓝屏日志 请问怎么处理呢     крокодил  трактор    президент  термометр  базар '
    

    If that works ok, then open a command prompt and run the program and redirect the output to files. Then open the files with notepad and see if they look ok.

    certmgr.exe -list -store uMY 1>c:\temp\stdout.txt  2>c:\temp\stderr.txt
    

    I am running Powershell 5.1 on Win10 21H1.

    One thing that I find strange, is that your example shows you calling the Execute-Command function before you define it. I get an error when I do that. This may be the result of running the script multiple times in ISE. It appears to call the function that you defined in the prior execution of the script. Move the statements that call the function below the definition of the function.

    0 comments No comments

  4. Андрей Михалевский 3,451 Reputation points
    2021-07-06T13:09:55.463+00:00

    $test = "крокодил трактор президент термометр базар"
    $test | out-file -FilePath C:\temp\test.txt

    I got a normal encoding with the Russian language.

    If I save the script in ps1 and run it in a normal powershell, there is no problem with the encoding. The only problem is with ISE

    0 comments No comments

  5. MotoX80 36,291 Reputation points
    2021-07-06T14:29:59.11+00:00

    The only problem is with ISE

    So this script produces unreadable characters? Do you have another PC that you can test on?

    cls  
    $test = "крокодил трактор президент термометр базар"  
    'The $test variable contains: {0}' -f $test    
    $test | out-file -FilePath C:\temp\test.txt  
    "Here is the contents of the test file."  
    get-content  C:\temp\test.txt  
    

    It works for me in both ISE and a PS prompt.

    112242-capture.jpg

    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.