I looked around on the Internet and can't find related information. My program needs to write some PowerShell code to the PowerShell profile. If there is no profile exists, my program will create one. The problem is what encoding should be used to create PowerShell profile. Should I use UTF8? Or UTF8 with BOM?
Asked
Active
Viewed 294 times
1
-
If you go by the xml files that come with powershell 5, some are unicode and some are utf8. In powershell core they're ascii or utf8 no bom. It probably doesn't matter. – js2010 Feb 12 '20 at 04:27
1 Answers
3
Technically the PowerShell profile uses the below encoding (from my own profile, 5.1).
BodyName : iso-8859-1
EncodingName : Western European (Windows)
HeaderName : Windows-1252
WebName : Windows-1252
Whereas a file with UTF-8 would appear like the below.
BodyName : utf-8
EncodingName : Unicode (UTF-8)
HeaderName : utf-8
WebName : utf-8
The difference between ASCII, ISO-8859-1 and UTF-8:
ASCII: 7 bits. 128 code points.
ISO-8859-1: 8 bits. 256 code points.
UTF-8: 8-32 bits. 1,112,064 code points.
This has previously been discussed at What is the difference between UTF-8 and ISO-8859-1?
You can check the encoding of a file by using the below.
$reader = [System.IO.StreamReader]::new("File path", [System.Text.Encoding]::default,$true)
$peek = $reader.Peek()
$reader.currentencoding
$reader.close()
Other encoding options that would work.
utf8: UTF-8 without BOM
utf8bom: UTF-8 with BOM
utf16le: Little endian UTF-16
utf16be: Big endian UTF-16
windows1252: Windows-1252

Drew
- 3,814
- 2
- 9
- 28
-
There is no PowerShell profile on a freshly installed machine. That means your profile is created by you. You can create it in iso-8859-1, but you can also create it in utf8 with BOM, right? So why you assume iso-8859-1 should be the right encoding to use? – Just a learner Feb 12 '20 at 04:17
-
Using ISO-8859-1 is the default format for anything prior to PowerShell 6. This was to ensure compatibility to legacy applications that may pre-date unicode. They have changed the default to UTF-8 (Without BOM) as of PowerShell 6. In short, do what you want and see what happens. I have updated my answer to include possible other encoding options. – Drew Feb 12 '20 at 04:29
-