Encoding issue when using Native CMD Command from

2019-06-26 00:34发布

When Executing native cmd command from powershell, e.g net view \\192.168.1.x I get the following results which non-English chars shown as ???? :

Share name        Type   Used as  Comment
--------------------------------------------------
AntEater          Disk
AntEater_Eab      Disk
HP  M4345 ?????   Print           HP  M4345 ?????
Letters           Disk
SMSCenter         Disk
temp              Disk
Users             Disk
????? ??? ??????  Print           ????? ??? ??????
The command completed successfully.

if I run chcp 862 I get the following results (on the console):

Share name        Type   Used as  Comment
--------------------------------------------------
AntEater          Disk
AntEater_Eab      Disk
HP  M4345 הנהחש   Print           HP  M4345 הנהחש
Letters           Disk
SMSCenter         Disk
temp              Disk
Users             Disk
מדפסת             Print           

Which is fine, but when I try to save it to a variable or export it to a file:

$temp = net view \\192.168.1.x
$temp

Share name        Type   Used as  Comment
--------------------------------------------------
AntEater          Disk
AntEater_Eab      Disk
HP  M4345 ?????   Print           HP  M4345 ?????
Letters           Disk
SMSCenter         Disk
temp              Disk
Users             Disk
????? ??? ??????  Print           ????? ??? ??????

the results are wrong again,

I've already tried:

net view \\192.168.1.x | Out-File C:\temp.txt -Encoding Unicode
cmd /c 'chcp 862 && net view \\192.168.1.x' | Out-File C:\temp.txt -Encoding Unicode
cmd /c 'chcp 862 && net view \\192.168.1.x > c:\temp.txt'
cmd /u /c 'chcp 862 && net view \\192.168.1.x > c:\temp.txt'

and much more really...

The Issue shown as well when running net view \\192.168.1.x | Out-String

The same results are whether I run it from CMD or PowerShell

Do you know the behavior? how can I save the correct output to a variable or file?

1条回答
贼婆χ
2楼-- · 2019-06-26 01:35

After changing the "language for non-Unicode programs" in the control panel, the problem solved, thanks for your help...

enter image description here

Run intl.cpl and select the Adminstrative tab.

查看更多
登录 后发表回答