Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct encoding for PS1 files

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?

like image 215
Gordon Avatar asked Jan 30 '17 15:01

Gordon


Video Answer


2 Answers

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.

like image 105
mschomm Avatar answered Oct 18 '22 15:10

mschomm


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
like image 25
Deadly-Bagel Avatar answered Oct 18 '22 15:10

Deadly-Bagel