Understanding file encoding in VS Code and PowerShell
文章推薦指數: 80 %
This problem occurs because VS Code encodes the character – in UTF-8 as the bytes 0xE2 0x80 0x93 . When these bytes are decoded as Windows-1252, ...
Skiptomaincontent
Thisbrowserisnolongersupported.
UpgradetoMicrosoftEdgetotakeadvantageofthelatestfeatures,securityupdates,andtechnicalsupport.
DownloadMicrosoftEdge
MoreinfoaboutInternetExplorerandMicrosoftEdge
Tableofcontents
Exitfocusmode
ReadinEnglish
Save
Tableofcontents
ReadinEnglish
Save
Feedback
Edit
Print
Twitter
LinkedIn
Facebook
Email
Tableofcontents
UnderstandingfileencodinginVSCodeandPowerShell
Article
10/22/2021
9minutestoread
1contributor
Inthisarticle
WhenusingVSCodetocreateandeditPowerShellscripts,itisimportantthatyourfilesaresaved
usingthecorrectcharacterencodingformat.
Whatisfileencodingandwhyisitimportant?
VSCodemanagestheinterfacebetweenahumanenteringstringsofcharactersintoabufferand
reading/writingblocksofbytestothefilesystem.WhenVSCodesavesafile,itusesatext
encodingtodecidewhatbyteseachcharacterbecomes.Formoreinformation,see
about_Character_Encoding.
Similarly,whenPowerShellrunsascriptitmustconvertthebytesinafiletocharactersto
reconstructthefileintoaPowerShellprogram.SinceVSCodewritesthefileandPowerShellreads
thefile,theyneedtousethesameencodingsystem.ThisprocessofparsingaPowerShellscript
goes:bytes->characters->tokens->abstractsyntaxtree->execution.
BothVSCodeandPowerShellareinstalledwithasensibledefaultencodingconfiguration.However,
thedefaultencodingusedbyPowerShellhaschangedwiththereleaseofPowerShell6.Toensureyou
havenoproblemsusingPowerShellorthePowerShellextensioninVSCode,youneedtoconfigureyour
VSCodeandPowerShellsettingsproperly.
Commoncausesofencodingissues
EncodingproblemsoccurwhentheencodingofVSCodeoryourscriptfiledoesnotmatchtheexpected
encodingofPowerShell.ThereisnowayforPowerShelltoautomaticallydeterminethefileencoding.
You'remorelikelytohaveencodingproblemswhenyou'reusingcharactersnotinthe
7-bitASCIIcharacterset.Forexample:
Extendednon-lettercharacterslikeem-dash(—),non-breakingspace()orleftdouble
quotationmark(")
Accentedlatincharacters(É,ü)
Non-latincharacterslikeCyrillic(Д,Ц)
CJKcharacters(本,화,が)
Commonreasonsforencodingissuesare:
TheencodingsofVSCodeandPowerShellhavenotbeenchangedfromtheirdefaults.ForPowerShell
5.1andbelow,thedefaultencodingisdifferentfromVSCode's.
Anothereditorhasopenedandoverwrittenthefileinanewencoding.Thisoftenhappenswiththe
ISE.
ThefileischeckedintosourcecontrolinanencodingthatisdifferentfromwhatVSCodeor
PowerShellexpects.Thiscanhappenwhencollaboratorsuseeditorswithdifferentencoding
configurations.
Howtotellwhenyouhaveencodingissues
Oftenencodingerrorspresentthemselvesasparseerrorsinscripts.Ifyoufindstrangecharacter
sequencesinyourscript,thiscanbetheproblem.Intheexamplebelow,anen-dash(–)appearsas
thecharactersâ€":
Send-MailMessage:Apositionalparametercannotbefoundthatacceptsargument'TestingFuseMailSMTP...'.
AtC:\Users\
延伸文章資訊
- 1Set-Content - PowerShell - SS64.com
set-content -encoding UTF8 will write a BOM if one is available in the source file, or if the sou...
- 2UTF-8 - MDN Web Docs Glossary: Definitions of Web-related terms
- 3批次將所有Java 原始碼檔案快速轉成UTF-8 編碼的方法
NET Framework 提供的 System.IO.File 類別,才能成功儲存以 UTF-8 編碼的文字檔案。 請注意: 以下程式執行到PowerShell 6+ 版本將會導致檔案內容整個...
- 4Get Text File Encoding - Power Tips - IDERA Community
That's why most cmdlets dealing with text file reading offer the ... Encoding]::UTF8} [PSCustomOb...
- 5Byte order mark - Globalization - Microsoft Learn