Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Character encoding (UTF-8) in PowerShell session [duplicate]

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.
like image 328
mrkskwsnck Avatar asked Aug 20 '18 14:08

mrkskwsnck


People also ask

How do I change my UTF-8 character set?

Click Tools, then select Web options. Go to the Encoding tab. In the dropdown for Save this document as: choose Unicode (UTF-8). Click Ok.

What is PowerShell default encoding?

In Windows PowerShell, the default encoding is usually Windows-1252, an extension of latin-1, also known as ISO 8859-1.


1 Answers

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 PowerShell

  • The $Home variable, which stores the current user's home directory

From the following article, if you wish to read more about this,
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.1&viewFallbackFrom=powershell-7.

like image 154
Shaqil Ismail Avatar answered Oct 16 '22 10:10

Shaqil Ismail