临时修改为utf-8
powershell
PS> chcp 65001PS> chcp 65001通过配置文件永久修改
查看powershell配置文件位置,如果不存在则去创建
powershell
PS> $PROFILEPS> $PROFILE在配置文件中写入
$OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding$OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding重新打开Powershell
如果出现报错如下
. : 无法加载文件 C:\Users\***\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1,因为在此系统上禁止运行脚本
。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 3
+ . 'C:\Users\gong\Documents\WindowsPowerShell\Microsoft.PowerShell_pro ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [],PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess. : 无法加载文件 C:\Users\***\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1,因为在此系统上禁止运行脚本
。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 3
+ . 'C:\Users\gong\Documents\WindowsPowerShell\Microsoft.PowerShell_pro ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [],PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess则使用管理员权限打开Powershell
执行命令如下,配置允许系统运行脚本,该配置默认是Restricted,默认禁止的
powershell
PS> Set-ExecutionPolicy RemoteSignedPS> Set-ExecutionPolicy RemoteSigned执行命令如下确认修改生效
powershell
PS> Get-ExecutionPolicyPS> Get-ExecutionPolicy重新打开powershell输入命令chcp,如果输出如下表示修改成功
Active code page: 65001Active code page: 65001输出重定向到文件时配置编码
在Powershell可以使用输出重定向,如下把ls命令重定向到文件aa.txt
powershell
PS> ls > aa.txtPS> ls > aa.txt之后使用记事本打开aa.txt,可以在右下角看到编码显示为UTF-16 LE
使用utf8重定向如下
powershell
PS> ls | out-file bb.txt -encoding utf8PS> ls | out-file bb.txt -encoding utf8用记事本打开bb.txt,看到编码显示带BOM的UTF-8