Hei all,
as a console/terminal enthusiast and database administrator (PostgreSQL) it is essential for me to work with the correct charcater encoding. Therefore, I want my client console/terminal window always set to e.g. UTF-8.
Back with Windows' CMD.EXE this attempt was as easy as typing the command chcp 65001
to set the desired 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
.