0

My powershell script needs to accept parameters with non-ASCII characters. And post it as Json to a API, but Powershell is converting all non-ASCII characters. How can I use the non-ASCII characters?

function Add-TestUser ([String] $name) {
   $body = @{name = $name}
   $JsonBody = $body | ConvertTo-Json
   write-host $name
   write-host $JsonBody
   #logic to call api
}

$name = "Caissière"
write-host $name

Add-TestUser -name $name

Output:

Caissière
Caissière
{
    "name":  "Caissière"
}

NOTE: script is run on a Windows Server

Gerrit Verhaar
  • 446
  • 4
  • 12
  • 2
    Don't use utf8 no bom encoding for the script. – js2010 Jan 27 '23 at 13:59
  • Since the problem occurs with _string literals_ in your _source code_, the likeliest explanation is that your script file is 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 the linked duplicate for details. – mklement0 Jan 27 '23 at 14:38
  • Thx for the direction to the answer. Never knew I could save the script with a different encoding. This helps. It did lead me to a new error. The JsonBody is now containing the non-ASCII character, but this results in a api error: Request parameter type error: JsonParseException: Invalid UTF-8 middle byte 0x72 Seems ConvertTo-Json needs a little help, as explained in: http://stackoverflow.com/questions/15290185/invoke-webrequest-issue-with-special-characters-in-json – Gerrit Verhaar Jan 30 '23 at 10:07

0 Answers0