Note: The next section applies primarily to Windows PowerShell. See the section after it for the cross-platform PowerShell Core (v6+) edition.
Home
Public
Questions
Tags
Users
Companies
Collectives
ExploreCollectives
Teams
StackOverflowforTeams
–Startcollaboratingandsharingorganizationalknowledge.
CreateafreeTeam
WhyTeams?
Teams
CreatefreeTeam
Collectives™onStackOverflow
Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost.
LearnmoreaboutCollectives
Teams
Q&Aforwork
Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch.
LearnmoreaboutTeams
ChangingPowerShell'sdefaultoutputencodingtoUTF-8
AskQuestion
Asked
5years,11monthsago
Modified
8daysago
Viewed
225ktimes
154
Bydefault,whenyouredirecttheoutputofacommandtoafileorpipeitintosomethingelseinPowerShell,theencodingisUTF-16,whichisn'tuseful.I'mlookingtochangeittoUTF-8.
Itcanbedoneonacase-by-casebasisbyreplacingthe>foo.txtsyntaxwith|out-filefoo.txt-encodingutf8butthisisawkwardtohavetorepeateverytime.
ThepersistentwaytosetthingsinPowerShellistoputthemin\Users\me\Documents\WindowsPowerShell\profile.ps1;I'veverifiedthatthisfileisindeedexecutedonstartup.
Ithasbeensaidthattheoutputencodingcanbesetwith$PSDefaultParameterValues=@{'Out-File:Encoding'='utf8'}butI'vetriedthisandithadnoeffect.
https://blogs.msdn.microsoft.com/powershell/2006/12/11/outputencoding-to-the-rescue/whichtalksabout$OutputEncodinglooksatfirstglanceasthoughitshouldberelevant,butthenittalksaboutoutputbeingencodedinASCII,whichisnotwhat'sactuallyhappening.
HowdoyousetPowerShelltouseUTF-8?
powershellutf-8character-encoding
Share
Improvethisquestion
Follow
editedMar22,2017at12:57
mklement0
333k5858goldbadges528528silverbadges658658bronzebadges
askedOct18,2016at2:54
rwallacerwallace
29.5k3434goldbadges116116silverbadges223223bronzebadges
Addacomment
|
3Answers
3
Sortedby:
Resettodefault
Highestscore(default)
Trending(recentvotescountmore)
Datemodified(newestfirst)
Datecreated(oldestfirst)
218
Note:
ThenextsectionappliesprimarilytoWindowsPowerShell.
Seethesectionafteritforthecross-platformPowerShellCore(v6+)edition.
Inbothcases,theinformationappliestomakingPowerShelluseUTF-8forreadingandwritingfiles.
Bycontrast,forinformationonhowtosendandreceiveUTF-8-encodedstringstoandfromexternalprograms,seethisanswer.
InPSv5.1orhigher,where>and>>areeffectivelyaliasesofOut-File,youcansetthedefaultencodingfor>/>>/Out-Fileviathe$PSDefaultParameterValuespreferencevariable:
$PSDefaultParameterValues['Out-File:Encoding']='utf8'
Note:
InWindowsPowerShell(thelegacyeditionwhoselatestandfinalversionisv5.1),thisinvariablycreatesUTF-8filewitha(pseudo)BOM.
ManyUnix-basedutilitiesdonotrecognizethisBOM(seebottom);seethispostforworkaroundsthatcreateBOM-lessUTF-8files.
InPowerShell(Core)v6+,BOM-lessUTF-8isthedefault(seenextsection),butifyoudowantaBOMthere,youcanuse'utf8BOM'
InPSv5.0orbelow,youcannotchangetheencodingfor>/>>,but,onPSv3orhigher,theabovetechniquedoesworkforexplicitcallstoOut-File.
(The$PSDefaultParameterValuespreferencevariablewasintroducedinPSv3.0).
InPSv3.0orhigher,ifyouwanttosetthedefaultencodingforallcmdletsthatsupport
an-Encodingparameter(whichinPSv5.1+includes>and>>),use:
$PSDefaultParameterValues['*:Encoding']='utf8'
Ifyouplacethiscommandinyour$PROFILE,cmdletssuchasOut-FileandSet-ContentwilluseUTF-8encodingbydefault,butnotethatthismakesitasession-globalsettingthatwillaffectallcommands/scriptsthatdonotexplicitlyspecifyanencodingviatheir-Encodingparameter.
Similarly,besuretoincludesuchcommandsinyourscriptsormodulesthatyouwanttobehavethesameway,sothattheyindeedbehavethesameevenwhenrunbyanotheruseroradifferentmachine;however,toavoidasession-globalchange,usethefollowingformtocreatealocalcopyof$PSDefaultParameterValues:
$PSDefaultParameterValues=@{'*:Encoding'='utf8'}
ForasummaryofthewildlyinconsistentdefaultcharacterencodingbehavioracrossmanyoftheWindowsPowerShellstandardcmdlets,seethebottomsection.
Theautomatic$OutputEncodingvariableisunrelated,andonlyappliestohowPowerShellcommunicateswithexternalprograms(whatencodingPowerShelluseswhensendingstringstothem)-ithasnothingtodowiththeencodingthattheoutputredirectionoperatorsandPowerShellcmdletsusetosavetofiles.
Optionalreading:Thecross-platformperspective:PowerShellCore:
PowerShellisnowcross-platform,viaitsPowerShellCoreedition,whoseencoding-sensibly-defaultstoBOM-lessUTF-8,inlinewithUnix-likeplatforms.
Thismeansthatsource-codefileswithoutaBOMareassumedtobeUTF-8,andusing>/Out-File/Set-ContentdefaultstoBOM-lessUTF-8;explicituseoftheutf8-EncodingargumenttoocreatesBOM-lessUTF-8,butyoucanopttocreatefileswiththepseudo-BOMwiththeutf8bomvalue.
IfyoucreatePowerShellscriptswithaneditoronaUnix-likeplatformandnowadaysevenonWindowswithcross-platformeditorssuchasVisualStudioCodeandSublimeText,theresulting*.ps1filewilltypicallynothaveaUTF-8pseudo-BOM:
ThisworksfineonPowerShellCore.
ItmaybreakonWindowsPowerShell,ifthefilecontainsnon-ASCIIcharacters;ifyoudoneedtousenon-ASCIIcharactersinyourscripts,savethemasUTF-8withBOM.
WithouttheBOM,WindowsPowerShell(mis)interpretsyourscriptasbeingencodedinthelegacy"ANSI"codepage(determinedbythesystemlocaleforpre-Unicodeapplications;e.g.,Windows-1252onUS-Englishsystems).
Conversely,filesthatdohavetheUTF-8pseudo-BOMcanbeproblematiconUnix-likeplatforms,astheycauseUnixutilitiessuchascat,sed,andawk-andevensomeeditorssuchasgedit-topassthepseudo-BOMthrough,i.e.,totreatitasdata.
Thismaynotalwaysbeaproblem,butdefinitelycanbe,suchaswhenyoutrytoreadafileintoastringinbashwith,say,text=$(catfile)ortext=$(/>>create"Unicode"-UTF-16LE-filesbydefault-inwhicheveryASCII-rangecharacter(too)isrepresentedby2bytes-whichnotablydiffersfromSet-Content/Add-Content(seenextpoint);New-ModuleManifestandExport-CliXmlalsocreateUTF-16LEfiles.
Set-Content(andAdd-Contentifthefiledoesn'tyetexist/isempty)usesANSIencoding(theencodingspecifiedbytheactivesystemlocale'sANSIlegacycodepage,whichPowerShellcallsDefault).
Export-CsvindeedcreatesASCIIfiles,asdocumented,butseethenotesre-Appendbelow.
Export-PSSessioncreatesUTF-8fileswithBOMbydefault.
New-Item-TypeFile-ValuecurrentlycreatesBOM-less(!)UTF-8.
TheSend-MailMessagehelptopicalsoclaimsthatASCIIencodingisthedefault-Ihavenotpersonallyverifiedthatclaim.
Start-TranscriptinvariablycreatesUTF-8fileswithBOM,butseethenotesre-Appendbelow.
Recommandsthatappendtoanexistingfile:
>>/Out-File-Appendmakenoattempttomatchtheencodingofafile'sexistingcontent.
Thatis,theyblindlyapplytheirdefaultencoding,unlessinstructedotherwisewith-Encoding,whichisnotanoptionwith>>(exceptindirectlyinPSv5.1+,via$PSDefaultParameterValues,asshownabove).
Inshort:youmustknowtheencodingofanexistingfile'scontentandappendusingthatsameencoding.
Add-Contentisthelaudableexception:intheabsenceofanexplicit-Encodingargument,itdetectstheexistingencodingandautomaticallyappliesittothenewcontent.Thanks,js2010.NotethatinWindowsPowerShellthismeansthatitisANSIencodingthatisappliediftheexistingcontenthasnoBOM,whereasitisUTF-8inPowerShellCore.
ThisinconsistencybetweenOut-File-Append/>>andAdd-Content,whichalsoaffectsPowerShellCore,isdiscussedinthisGitHubissue.
Export-Csv-Appendpartiallymatchestheexistingencoding:itblindlyappendsUTF-8iftheexistingfile'sencodingisanyofASCII/UTF-8/ANSI,butcorrectlymatchesUTF-16LEandUTF-16BE.
Toputitdifferently:intheabsenceofaBOM,Export-Csv-AppendassumesUTF-8is,whereasAdd-ContentassumesANSI.
Start-Transcript-Appendpartiallymatchestheexistingencoding:ItcorrectlymatchesencodingswithBOM,butdefaultstopotentiallylossyASCIIencodingintheabsenceofone.
Cmdletsthatread(thatis,theencodingusedintheabsenceofaBOM):
Get-ContentandImport-PowerShellDataFiledefaulttoANSI(Default),whichisconsistentwithSet-Content.
ANSIisalsowhatthePowerShellengineitselfdefaultstowhenitreadssourcecodefromfiles.
Bycontrast,Import-Csv,Import-CliXmlandSelect-StringassumeUTF-8intheabsenceofaBOM.
Share
Improvethisanswer
Follow
editedNov25,2021at14:26
answeredOct18,2016at3:12
mklement0mklement0
333k5858goldbadges528528silverbadges658658bronzebadges
21
1
IsthereanywaytoforcetonotprependteBOMonWin10?
– mvorisek
Jan30,2019at0:27
1
@Mvorisek:InWindowsPowerShell,youcan't-youhavetorollyourownoutputfunction-seestackoverflow.com/a/34969243/45375.InPowerShellCore(alsoonWindows),BOM-lessisthedefault.
– mklement0
Jan30,2019at3:13
2
Idon'tdisagree,@EliaWeiss,butit'sWindowsPowerShellspecifically,andtheyeventuallydidgetitrightinPowerShellCore.
– mklement0
Apr12,2019at16:38
2
@Marc:VSCodeandothermoderncross-platformeditorscommendablydefaulttoUTF-8,which,however,meansthey'llmisinterpretANSI-encodedfiles.Notepadusesheuristicstoguesstheencoding.Thepointisthatitisonlyaguess,becauseanyUTF-8-encodedfileisalsoatechnicallyvalidANSI-encodedfile(butnotviceversa).ItwouldbegreatifeverythingonWindowsdefaultedtoUTF-8intheabsenceofaBOMthewayUnix-likeplatformsdo,butthat'snotthecase,notablynotinWindowsPowerShell,thoughfortunatelyitisnowthecaseinPowerShellCore.
– mklement0
Apr17,2019at22:16
2
Towatchyourcurrentvalueifsome,justtype$PSDefaultParameterValues
– Sandburg
Nov7,2019at13:53
|
Show16morecomments
3
Tobeshort,use:
write-output"yourtext"|out-file-append-encodingutf8"filename"
Youmaywanttoputpartsofthescriptintobracessoyoucouldredirectoutputoffewcommands:
{
command1
command2
}|out-file-append-encodingutf8"filename"
Share
Improvethisanswer
Follow
editedMay16,2021at2:04
answeredMay24,2020at15:17
pbiespbies
6341010silverbadges2525bronzebadges
2
1
Toquotefromthequestion:"Itcanbedoneonacase-by-casebasisbyreplacingthe>foo.txtsyntaxwith|out-filefoo.txt-encodingutf8butthisisawkwardtohavetorepeateverytime."Inotherwords:you'resuggestingpreciselywhattheOPistryingtoavoid.
– mklement0
Feb16,2021at21:34
1
ithink-appendshouldberemoved
– SashaBond
Mar3,2021at0:16
Addacomment
|
1
AdumpmadeusingPowerShellonWindowswithoutputredirectioncreatesafilethathasUTF-16encoding.Toworkaroundthisissue,youcantry:
mysqldump.exe[options]--result-file=dump.sql
Referencelink:mysqldump_result-file
Share
Improvethisanswer
Follow
answeredOct3at9:20
PhátNguyễnThànhPhátNguyễnThành
1111bronzebadge
Addacomment
|
YourAnswer
ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers.
Draftsaved
Draftdiscarded
Signuporlogin
SignupusingGoogle
SignupusingFacebook
SignupusingEmailandPassword
Submit
Postasaguest
Name
Email
Required,butnevershown
PostYourAnswer
Discard
Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy
Nottheansweryou'relookingfor?Browseotherquestionstaggedpowershellutf-8character-encodingoraskyourownquestion.
TheOverflowBlog
HowtoearnamillionreputationonStackOverflow:beofservicetoothers
Therightwaytojobhop(Ep.495)
FeaturedonMeta
BookmarkshaveevolvedintoSaves
Inboximprovements:markingnotificationsasread/unread,andafiltered...
Revieweroverboard!Orarequesttoimprovetheonboardingguidancefornew...
CollectivesUpdate:RecognizedMembers,Articles,andGitLab
Shouldweburninatethe[script]tag?
Linked
-1
mysqldumpandpowershellproducesutf-16
0
DifferencebetweenPowershell5.1and7.0forloggingandregexfiltering
0
powershelltypecommandcausinginvalidcharacters
0
isthereadifferenceinwindowsvs.linuxforrenderingnon-latincharacterstoconsole?
329
UsingPowerShelltowriteafileinUTF-8withouttheBOM
193
WhydoesGittreatthistextfileasabinaryfile?
74
DisplayingUnicodeinPowershell
29
MergingmultipleCSVfilesintooneusingPowerShell
23
Usingredirectionwithinthescriptproducesaunicodeoutput.Howtoemitsingle-byteASCIItext?
21
Gitapplyerror:unrecognizedinput
Seemorelinkedquestions
Related
597
Bestwaytoconverttextfilesbetweencharactersets?
1322
UTF-8allthewaythrough
397
SettingthedefaultJavacharacterencoding
2334
HowdoIgetaconsistentbyterepresentationofstringsinC#withoutmanuallyspecifyinganencoding?
674
WhatisthedifferencebetweenUTF-8andUnicode?
327
DetectencodingandmakeeverythingUTF-8
974
What'sthedifferencebetweenUTF-8andUTF-8withBOM?
585
WhydoesmodernPerlavoidUTF-8bydefault?
49
UTF-8outputfromPowerShell
HotNetworkQuestions
HowradioactivecouldImakesomematerials,whilekeepingthem"cold"?
Howtoremovetikznode?
WhathappenswhenthequasarremnantsreachEarthin3millionyears?
IfthedrowshadowbladeusesShadowSwordasarangedattack,doesitthrowasword(thatitthenhastoretrievebeforeusingitagain)?
Areopeningandclosingstatementspartoftherecord?
Whatisthebestwaytocalculatetruepasswordentropyforhumancreatedpasswords?
ShouldIusepwdortildeplus(~+)?
Whatdoes"parameterizedby"mean?
Howtotellifmybikehasanaluminumframe
Determinethelengthoftherestofamathdisplaylineformultlined
UnderstandingElectricFieldsLinesandhowtheyshow‘like’chargesrepelling
HowdothosewhoholdtoaliteralinterpretationofthefloodaccountrespondtothecriticismthatNoahbuildingthearkwouldbeunfeasible?
I2C(TWI)vsSPIEMInoiseresistance
keyless/flatkeyboard
DidMS-DOSeverdropabilitytosupportnon-IBMPCcompatiblemachines?
Whatisthedefinitionofatrollinthelegalcontext?
Probabilisticmethodsforundecidableproblem
ConvertanintegertoIEEE754float
Howtosimplifyapurefunction?
WhyamIgettingasortwhenIhaveanindex?
IsdocumentingabigprojectwithUMLDiagramsneeded,goodtohaveorevennotpossible?
circuitikz:Addingarrowheadtotapofvariableinductance?
PacifistethosblockingmyprogressinStellaris
Wouldextractinghydrogenfromthesunlessenitslifespan?
morehotquestions
Questionfeed
SubscribetoRSS
Questionfeed
TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader.
default
Yourprivacy
Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy.
Acceptallcookies
Customizesettings