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\
延伸文章資訊
- 1Change / Save encoding How to convert several txt files UTF ...
Hello. I need to convert several txt files , located in C:\Folder1 from UTF-8 to UTF-8-BOM. I had...
- 2批次將所有Java 原始碼檔案快速轉成UTF-8 編碼的方法
NET Framework 提供的 System.IO.File 類別,才能成功儲存以 UTF-8 編碼的文字檔案。 請注意: 以下程式執行到PowerShell 6+ 版本將會導致檔案內容整個...
- 3Understanding file encoding in VS Code and PowerShell
This problem occurs because VS Code encodes the character – in UTF-8 as the bytes 0xE2 0x80 0x93 ...
- 4PowerShell Studio tip: View and change file encoding
- 5Changing source files encoding and some fun with PowerShell
Changing source files encoding and some fun with PowerShell ... At least it can correctly read te...