从Powershell使用Native CMD Command时的编码问题

时间:2015-12-29 12:05:31

标签: powershell unicode encoding cmd

从powershell执行本机cmd命令时,例如net view \\192.168.1.x我得到以下结果,非英语字符显示为????

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.

如果我运行chcp 862,我会得到以下结果(在控制台上):

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           

哪个好,但是当我尝试将其保存到变量或将其导出到文件时:

$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           ????? ??? ??????

结果再次出错,

我已经尝试过了:

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'

还有更多......

运行net view \\192.168.1.x | Out-String

时也会显示问题

同样的结果是我是从CMD还是PowerShell

运行它

你知道这个行为吗?如何将正确的输出保存到变量或文件?

1 个答案:

答案 0 :(得分:1)

更改非Unicode程序的"语言"在控制面板中,问题解决了,谢谢你的帮助...

enter image description here

运行intl.cpl并选择Adminstrative标签。