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\
延伸文章資訊
- 1How to: Save and open files with encoding - Visual Studio (Windows)
- 2PowerShell Studio tip: View and change file encoding
- 3Set-Content - PowerShell - SS64.com
set-content -encoding UTF8 will write a BOM if one is available in the source file, or if the sou...
- 4Read UTF-8 files correctly with PowerShell - Stack Overflow
I need a function that can read any file with UTF-8 encoding, ignore and delete the BOM and not m...
- 5Get-Content - PowerShell Command - PDQ
UTF8: Encodes in UTF-8 format. ... Therefore, by default, when reading a text file, Get-Content r...