What is the correct encoding for PS1 files

I handle text stream in a series of PS1 and PSM1 files, and I ran into some problems with smart quotes and an em dash (never, NEVER, cut or pasted code from the MS Scripting Guy blog). I realized that the problem was encoding, so I looked, and I have both ASCII and UTF8 files, but of course, both have problems with my funk text. So I made some replacements, and I have work, but I wonder if one encoding should also be standardized, and if so, which one?

+5
source share
2 answers

Not a direct answer to your question, but you can find it useful nonetheless. I have a tool that I wrote for processing PS and SQL scripts, but quickly discovered that people stick their emails, which screwed a ton of things. I had to do this to fix everything, and he 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
source

Since I just ran into this when fixing encoding problems: I had some PS1 files encoded as UTF-8, and the powershell interpreter suffocated from some constant lines containing German umlaut characters ("äÄöÖüÜß"). Got an "unexpected token" error (starting PS 5.1 on Windows Server 2016). This particular problem disappeared after changing the encoding of the PS1 file to UTF-8-BOM.

0

Source: https://habr.com/ru/post/1668273/


All Articles