PowerShell会话中的字符编码(UTF-8) [英] Character encoding (UTF-8) in PowerShell session
问题描述
大家好,
作为控制台/终端爱好者和数据库管理员(PostgreSQL),使用正确的字符编码对我来说至关重要.因此,我希望我的客户端控制台/终端窗口始终设置为例如UTF-8.
使用Windows的CMD.EXE返回此尝试就像键入命令 预先感谢 TheIncorrigible1 的第二条评论使我找到了迄今为止的最佳答案:解决方案 chcp 65001
来设置所需的/EDIT
您可以使用PowerShell配置文件.PowerShell支持多个配置文件.此外,PowerShell主机程序可以支持其自己的特定于主机的配置文件.
例如,PowerShell控制台支持以下基本配置文件.配置文件按优先顺序列出.第一个配置文件具有最高优先级.
配置文件说明路径所有用户,所有主机$ PSHOME \ Profile.ps1所有用户,当前主机$ PSHOME \ Microsoft.PowerShell_profile.ps1当前用户,所有主机$ Home \ [我的] Documents \ PowerShell \ Profile.ps1当前用户,当前主机$ Home \ [我的] Documents \ PowerShell \ Microsoft.PowerShell_profile.ps1
配置文件路径包含以下变量:
-
$ PSHOME
变量,用于存储PowerShell的安装目录 -
$ Home
变量,用于存储当前用户的主目录
从下面的文章中,如果您想了解更多有关此的信息,
code page identifier.
Now, I am in the process of switching to PowerShell and setting the character encoding seems very odd, IMHO.
I've done some research on how to set the PowerShell session to UTF-8 and I figured out, that I need three steps/commmnds to accomplish that.
PS C:\> $OutputEncoding = [System.Text.Encoding]::UTF8
PS C:\> [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
PS C:\> chcp 65001
Despite the fact that the first two commands are not intuitive and hard to remember... Leaving out one of them leads to something not working out properly! Also, setting just one of them seems to have no effect to the others.
So, I must set all three for working with the PostgreSQL's psql
database client.
Otherwise I run into encoding issues while exporting/importing data.
Now my question is: "Why the heck? Isn't there an easier way to simply set the character encoding in PowerShell?" Unfortunately, I did not find any plausible documentation myself about setting the character enconding!
Thanks in advance
/EDIT
The second comment by TheIncorrigible1 led me to the best answer fo far: Displaying Unicode in Powershell - So one can set the whole PowerShell with two separated statements to the desired encoding (UTF-8).
PS C:\> $OutputEncoding = [System.Console]::OutputEncoding = [System.Console]::InputEncoding = [System.Text.Encoding]::UTF8
PS C:\> $PSDefaultParameterValues['*:Encoding'] = 'utf8'
Explanation:
$OutputEncoding
sets the encoding for e.g.|
(piping) and/or communication between programs and/or processes.[System.Console]::OutputEncoding
sets the encoding for STDOUT and the console/terminal output.[System.Console]::InputEncoding
sets the encoding for STDIN or keyboard input.$PSDefaultParameterValues['*:Encoding']
sets the encoding for all cmdlets that support the-Encoding
option like e.g.Out-File -Encoding
.
You could use PowerShell profiles. PowerShell supports several profile files. Also, PowerShell host programs can support their own host-specific profiles.
For example, the PowerShell console supports the following basic profile files. The profiles are listed in precedence order. The first profile has the highest precedence.
THE PROFILE FILES
Description Path
All Users, All Hosts $PSHOME\Profile.ps1
All Users, Current Host $PSHOME\Microsoft.PowerShell_profile.ps1
Current User, All Hosts $Home\[My ]Documents\PowerShell\Profile.ps1
Current user, Current Host $Home\[My ]Documents\PowerShell\Microsoft.PowerShell_profile.ps1
The profile paths include the following variables:
The
$PSHOME
variable, which stores the installation directory for PowerShellThe
$Home
variable, which stores the current user's home directory
From the following article, if you wish to read more about this,
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.1&viewFallbackFrom=powershell-7.
这篇关于PowerShell会话中的字符编码(UTF-8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!