powershellcommand-line-interface

non-ASCII characters in CLI output in powershell script


How can I retrieve users with non-latin names from this output?

.\Pacli.exe USERSLIST INCLUDESUBLOCATIONS=YES output`(name`, enclose`, type`) > users.txt

This saves and recalls the non-latin characters as ? or even with Get-Content -Encoding UTF8.

I tried to set

$OutputEncoding = [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding

before this command but got the same result.


Solution

  • tl;dr

    Use the code at the bottom to temporarily change [Console]::OutputEncoding to match PACLI.exe's nonstandard output encoding, which appears to be ANSI, to ensure that its output is decoded correctly.


    Per your own feedback, it turns out that PACLI.exe exhibits nonstandard behavior and outputs Windows-1252-encoded text.


    Capturing output from external programs that use nonstandard character encodings:

    Capturing output from a CLI that outputs ANSI, like (presumably) PACLI.exe and Python, using your specific PACLI.exe call:

    & {
      # Temporarily change the expected output encoding to ANSI.
      $prevEnc = [Console]::OutputEncoding
      [Console]::OutputEncoding = 
        [Text.Encoding]::GetEncoding(
          [int] (Get-ItemPropertyValue HKLM:\SYSTEM\CurrentControlSet\Control\Nls\CodePage ACP)
        )
    
      try {
    
       .\Pacli.exe USERSLIST INCLUDESUBLOCATIONS=YES output`(name`, enclose`, type`) |
          Set-Content -Encoding utf8 users.txt
    
      } finally {
    
        # Restore the original encoding.
        [Console]::OutputEncoding = $prevEnc
    
      }
    }
    

    [1] Except in CJK system locales.

    [2] Unfortunately, workarounds are required to create BOM-less UTF-8 files in Windows PowerShell (which PowerShell 7 creates by default) - see this answer.