14

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.
mrkskwsnck
  • 338
  • 1
  • 3
  • 14
  • @mklement0 leaves an excellent answer in the flagged duplicate post describing powershell & its encoding woes. – Maximilian Burszley Aug 20 '18 at 15:29
  • 1
    I flagged the wrong answer: [here's the one I meant to use](https://stackoverflow.com/questions/49476326/displaying-unicode-in-powershell) – Maximilian Burszley Aug 20 '18 at 15:35
  • So is this persistent between restarting (power/core) shells, or does it have to be added to a profile? – not2qubit Dec 18 '20 at 21:07
  • 1
    Note that the linked answer uses `New-Object System.Text.UTF8Encoding` (equivalent: `[System.Text.UTF8Encoding]::new()`), and not `[System.Text.Encoding]::UTF8`, as the latter _uses a BOM_, which you want to avoid with `$OutputEncoding` – mklement0 Dec 18 '20 at 21:12
  • 1
    @not2qubit: No, it doesn't persist automatically - you'll have to add it to your `$PROFILE`. – mklement0 Dec 18 '20 at 21:13
  • If setting the code pages to `65001` (UTF-8) _system-wide_ is an option - which has far-reaching consequences - you'll consistently get BOM-less UTF-8 _in PowerShell [Core] v6+_ - see [this answer](https://stackoverflow.com/a/57134096/45375). However, in _Windows PowerShell_ you'll still need to set `$OutputEncoding` and `$PSDefaultParameterValues['*:Encoding']`, and note that when files are created, those files will invariably be UTF-8 _with BOM_. – mklement0 Dec 18 '20 at 21:30

1 Answers1

1

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.

Shaqil Ismail
  • 1,794
  • 1
  • 4
  • 5