Skip to content

Instantly share code, notes, and snippets.

@xoner
Created January 30, 2013 07:54
Show Gist options
  • Star 46 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save xoner/4671514 to your computer and use it in GitHub Desktop.
Save xoner/4671514 to your computer and use it in GitHub Desktop.
Set the default en coding to UTF-8 in powershell. As seen in http://blogs.msdn.com/b/powershell/archive/2006/12/11/outputencoding-to-the-rescue.aspx
$OutputEncoding = New-Object -typename System.Text.UTF8Encoding
[Console]::OutputEncoding = New-Object -typename System.Text.UTF8Encoding
@Tiliavir
Copy link

first line is irrelevant, right?

@Velocet
Copy link

Velocet commented Sep 30, 2016

I prefer writing it this way:

[Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8

Another way is to use chcp.com:

& "$env:windir\system32\chcp.com" 65001

@isalgueiro
Copy link

@Tiliavir $OutputEncoding controlls how text is passed through pipes https://blogs.msdn.microsoft.com/powershell/2006/12/11/outputencoding-to-the-rescue/

@Memnarch
Copy link

Memnarch commented Apr 3, 2018

Just leaving this as an alternative if it is required to start a Powershell childprocess instantly as UTF8 (including pipes) without running an external script:
Powershell.exe -NoExit -Command "$OutputEncoding = [Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8"

Copy link

ghost commented Jun 13, 2018

Add $env:LC_ALL='C.UTF-8' to your $profile.

Copy link

ghost commented Mar 24, 2019

"$OutputEncoding = [Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8"
This works...

@brunorozendo
Copy link

Add $env:LC_ALL='C.UTF-8' to your $profile.

👍

@nexssp
Copy link

nexssp commented Apr 14, 2021

"$OutputEncoding = [Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8" works for me.

I have tried:

  • "$PSDefaultParameterValues = @{'Out-File:Encoding' = 'utf8'}" didn't work
  • "$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'" didn't work.

@RomanDovgii
Copy link

[Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8

God bless your soul!

@pierricgimmig
Copy link

$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'

worked for me for PowerShell 5.1, as per this doc

@pierricgimmig
Copy link

Ended up using $PSDefaultParameterValues['*:Encoding'] = 'utf8' to set utf-8 to "to change the default encoding for all cmdlets that have the Encoding parameter". Also put it in the $profile file for persistence.

@DigitalDwagon
Copy link

You can also add [Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8 to the start of your powershell profile script.

Notepad $profile
If the file doesn't exist, create one
add [Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8 as the first line of the file`
save
restart powershell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment