What is the correct encoding for PS1 files

爷,独闯天下 提交于 2020-12-29 13:13:00

问题


I am doing some text stream processing on a series of PS1 & PSM1 files, and I ran into some issues with smart quotes and em-dashes (never, NEVER, cut and paste code from MS Scripting Guy blog). I figured the issue was encoding so I looked, and I have files of both ASCII & UTF8, but of course both have issues with my funky text. So I have done some replacements, and I have that working, but I wonder if I shouldn't also standardize on one encoding, and if so, which one?


回答1:


Not a direct answer to your question but you may find it useful nonetheless, I have a tool I wrote to handle PS and SQL scripts but quickly found people were pasting from their emails which screwed a ton of stuff. I had to implement this to correct it all, and it should get everything:

if ($code.IndexOf([Char]0x2013) -gt -1) { $code = $code.Replace(([Char]0x2013).ToString(), "--") }   # en dash
if ($code.IndexOf([Char]0x2014) -gt -1) { $code = $code.Replace(([Char]0x2014).ToString(), "-") }    # em dash
if ($code.IndexOf([Char]0x2015) -gt -1) { $code = $code.Replace(([Char]0x2015).ToString(), "-") }    # horizontal bar
if ($code.IndexOf([Char]0x2017) -gt -1) { $code = $code.Replace(([Char]0x2017).ToString(), "_") }    # double low line
if ($code.IndexOf([Char]0x2018) -gt -1) { $code = $code.Replace(([Char]0x2018).ToString(), "`'") }   # left single quotation mark
if ($code.IndexOf([Char]0x2019) -gt -1) { $code = $code.Replace(([Char]0x2019).ToString(), "`'") }   # right single quotation mark
if ($code.IndexOf([Char]0x201a) -gt -1) { $code = $code.Replace(([Char]0x201a).ToString(), ",") }    # single low-9 quotation mark
if ($code.IndexOf([Char]0x201b) -gt -1) { $code = $code.Replace(([Char]0x201b).ToString(), "`'") }   # single high-reversed-9 quotation mark
if ($code.IndexOf([Char]0x201c) -gt -1) { $code = $code.Replace(([Char]0x201c).ToString(), "`"") }   # left double quotation mark
if ($code.IndexOf([Char]0x201d) -gt -1) { $code = $code.Replace(([Char]0x201d).ToString(), "`"") }   # right double quotation mark
if ($code.IndexOf([Char]0x201e) -gt -1) { $code = $code.Replace(([Char]0x201e).ToString(), "`"") }   # double low-9 quotation mark
if ($code.IndexOf([Char]0x2026) -gt -1) { $code = $code.Replace(([Char]0x2026).ToString(), "...") }  # horizontal ellipsis
if ($code.IndexOf([Char]0x2032) -gt -1) { $code = $code.Replace(([Char]0x2032).ToString(), "`"") }   # prime
if ($code.IndexOf([Char]0x2033) -gt -1) { $code = $code.Replace(([Char]0x2033).ToString(), "`"") }   # double prime
if ($code.IndexOf([Char]0x0009) -gt -1) { $code = $code.Replace(([Char]0x0009).ToString(), "    ") } # tab



回答2:


Since I just ran into this while troubleshooting encoding problems: I had some PS1 files encoded as UTF-8 and the powershell interpreter choked on some constant strings containing German umlaut characters ("äÄöÖüÜß"). Got an 'unexpected token' error (running PS 5.1 on Windows Server 2016). This particular problem vanished after changing the encoding of the PS1 file to UTF-8-BOM.




回答3:


PS1 Files historically used Windows-1252 as the fallback encoding. If a BOM was present (either for UTF-8 or for UTF-16), it would be used instead.

With Powershell 6, the default encoding changes to UTF-8.

To make sure ps1 files work correctly on old and new versions, save them as UTF-8 with BOM. This advice can be found in about_Character_Encoding



来源:https://stackoverflow.com/questions/41939799/what-is-the-correct-encoding-for-ps1-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!