1

I am trying to execute a PowerShell script in CMD/PowerShell Prompt.

Please find my PS Script below:

[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$name= "François Remetter"
write-host $name

But in CMD and PowerShell Prompt output ç is getting replaced by ç.

I am getting the below output:

D:\Codes\PSScripts>powershell exec.ps1
François Remetter

I tried to change the code page to 1252 and 65001.

But still I am getting the same wrong output.

Can someone please help me to resolve the issue ?

Sayandip Ghatak
  • 191
  • 2
  • 14
  • Depending on your version of PowerShell, you also need to set the `$OutputEncoding` global variable to match `[Console]::OutputEncoding` – Maximilian Burszley Aug 18 '23 at 17:30
  • In my own profile script, I usually set these both to `[Text.Encoding]::UTF8` as well as `chcp 65001` – Maximilian Burszley Aug 18 '23 at 17:30
  • I had a [related problem](https://stackoverflow.com/questions/47774166/snmp-response-mangled-by-powershell); for me, the correct encoding was `[system.text.encoding]::Default` rather than any particular encoding such as `UTF8` or `ASCII`. – Jeff Zeitlin Aug 18 '23 at 18:24
  • 2
    The likeliest explanation is that there's a problem with your _script file's character encoding_, causing it to be misinterpreted by the Windows PowerShell engine, which happens if the script is saved as UTF-8 _without a BOM_. Try saving your script as UTF-8 _with BOM_. See [this answer](https://stackoverflow.com/a/54790355/45375) for details. If this solves your problem, we can close your question as duplicate. – mklement0 Aug 18 '23 at 21:26
  • @mklement0 I couldn't reproduce the OP's problem on Windows 10, but I just tried your suggestion. For me the text renders correctly regardless of the BOM setting (i.e. with/without BOM). – skomisa Aug 18 '23 at 22:29
  • Although I can't reproduce your problem, I noticed that when I opened a PowerShell window this message was displayed: _"Try the new cross-platform PowerShell https://aka.ms/pscore6"_. If you also see that message you might consider going to that link and obtaining a newer version of PowerShell to see if that helps. You can determine your current version of PowerShell by submitting `$PSVersionTable` from the PowerShell window. If you are still stuck then update your question to state the version(s) of PowerShell that you have used. – skomisa Aug 18 '23 at 22:35
  • 1
    @skomisa, indeed the problem is specific to _Windows PowerShell_, the legacy, Windows-only, ships-with-Windows edition of PowerShell, as opposed to the modern, cross-platform, install-on-demand edition, _PowerShell (Core) 7+_ - as explained in the linked answer. – mklement0 Aug 18 '23 at 22:59
  • @mklement0 OK. FWIW, my Windows 10 version of PowerShell is 5.1.19041.3031. I presume that is the _"ships-with-Windows"_ version since I have never explicitly updated PowerShell. Since I can't reproduce the problem, is it possible that the OP's locale is relevant? – skomisa Aug 19 '23 at 00:36
  • 1
    @skomisa, yes, v5.1.x is the latest _and last_ version of _Windows PowerShell_. v6+ is _PowerShell (Core)_. You can reproduce the problem in _Windows PowerShell_ by saving the script content from the question in a UTF-8 file _without a BOM_, as stated. You would only _not_ see the problem if you've explicitly configured your system to use UTF-8 _system-wide_, as described in [this answer](https://stackoverflow.com/a/57134096/45375). – mklement0 Aug 19 '23 at 01:24
  • 1
    @mklement0 Thanks !!!!! I changed the script encoding from UTF8 to UTF8 with BOM and I am getting the correct output. Thanks for your help. – Sayandip Ghatak Aug 19 '23 at 07:23
  • 1
    @mklement0 Everything you stated was correct. I had been using the _"UTF-8"_ locale, but once I switched my locale to _"English (United States)"_ and saved the script file without a BOM I could reproduce the OP's problem. – skomisa Aug 19 '23 at 10:07

0 Answers0