I have a Powershell script that I run remotely. I'm trying to add a value to the register. But letters with special accents are replaced with something else.
When I run it locally on the machine the letters are correctly added in the register.
When I remotely write the output to console and check the logs, I can see it works.
As a test I run these commands directly after each other:
write-host("Issuer: " + $Issuer)
Set-Itemproperty -path 'HKLM:\SYSTEM\Test' -Name 'Issuer' -value $Issuer
Result:
The write-host returns the correct letters. The register change keeps changing it to the wrong characters. I guess it has to do something with the user that remotely runs the script not having the right default charset. So I would like to define this in the script.
I have tried following without result:
- Adding chcp 65001 to the script.
- Running it with a CMD.EXE command: cmd.exe /c "REG ADD HKLM\System\Test /v ""Issuer"" /t REG_SZ /d ""$Issuer"" /F"
- Adding chcp 65001 to the CMD command.
- Tried running CMD with "cmd.exe [Text.Utf8Encoding] /c" , but can't find much info supporting this.
- ...
I had the same problem when sending a mail via Powershell. Here the characters in the mail were replaced with "?". This I could solve by adding " -Encoding ([System.Text.Encoding]::UTF8)" to the command. But I can't find a way to do this for Set-Itemproperty.
Edit: I found this answer to be somewhat helpful: Using UTF-8 Encoding (CHCP 65001) in Command Prompt / Windows Powershell (Windows 10)
When I go to "Change System Locale" and I check the beta option "Use Unicode UTF-8 for worldwide language support", everything works. So I narrowed down where the problem lies. I just don't want to enable this beta option for all our machines since I read that it might inflict with other applications.
Strange thing is. When I output the $OutputEncoding I have following output:
Normal
(not working)
-------------
IsSingleByte : True
BodyName : us-ascii
EncodingName : US-ASCII
HeaderName : us-ascii
WebName : us-ascii
WindowsCodePage : 1252
IsBrowserDisplay : False
IsBrowserSave : False
IsMailNewsDisplay : True
IsMailNewsSave : True
EncoderFallback : System.Text.EncoderReplacementFallback
DecoderFallback : System.Text.DecoderReplacementFallback
IsReadOnly : True
CodePage : 20127
With following line in the script: $OutputEncoding = [Console]::OutputEncoding = New-Object System.Text.Utf8Encoding
(not working)
-------------
BodyName : utf-8
EncodingName : Unicode (UTF-8)
HeaderName : utf-8
WebName : utf-8
WindowsCodePage : 1200
IsBrowserDisplay : True
IsBrowserSave : True
IsMailNewsDisplay : True
IsMailNewsSave : True
IsSingleByte : False
EncoderFallback : System.Text.EncoderReplacementFallback
DecoderFallback : System.Text.DecoderReplacementFallback
IsReadOnly : True
CodePage : 65001
With System wide UTF Change (via intl.cpl > Administrative > Change System locale > Checkbox
(working)
---------------------------
IsSingleByte : True
BodyName : us-ascii
EncodingName : US-ASCII
HeaderName : us-ascii
WebName : us-ascii
WindowsCodePage : 1252
IsBrowserDisplay : False
IsBrowserSave : False
IsMailNewsDisplay : True
IsMailNewsSave : True
EncoderFallback : System.Text.EncoderReplacementFallback
DecoderFallback : System.Text.DecoderReplacementFallback
IsReadOnly : True
CodePage : 20127
As you can see the $OutputEncoding hasn't changed when enabling the system wide option. What did it change? Can I change it via powershell before making my registry change and change it back after?
