Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, November 1, 2018 2:07 AM
Hi,
I have this string in a Powershell script and want to show it with the corresponding accents. What do I have to do?
$originalString = "Fuentes de información"
Desired output: "Fuentes de información"
Any help would be really appreciated.
Ricardo
All replies (15)
Thursday, November 1, 2018 2:39 AM
The second string display correctly in PowerShell. The first string displays correctly. The strings contain different characters. YOU can only change that by editing the string.
The first string was converted from Unicode incorrectly and now contains two characters instead of one.
Check the length of both strings.
This has to be fixed in the source system or file and cannot be fixed in PowerShell.
Here is the proof.
PS D:\scripts> ("Fuentes de información").Length
23
PS D:\scripts> ("Fuentes de información").Length
22
PS D:\scripts>
\(ツ)_/
Thursday, November 1, 2018 2:45 AM
Thank you jrv
I used this code:
$string = "Fuentes de información"
$a = $string -replace "ó", "ó"
Write-Host $a
and the output is:
PS E:\Temp> .\pruebaPS.ps1
Fuentes de información
What am I doing wrong? is it the encoding?
Thursday, November 1, 2018 3:02 AM
That has absolutely nothing to do with what you asked, Of course you vcan manually replace any characters with any other characters. You can also do that in notepad.
Encoding/Decoding are a completely different technology.
\(ツ)_/
Thursday, November 1, 2018 3:15 AM
Poweshell still outputs the original characters. Even though I replaced them. So I believe my question is still valid. How can I force powershell to output the accent?
Thursday, November 1, 2018 3:22 AM
Works fine for me:
PS D:\scripts> $t = "Fuentes de información" -replace 'ó','ó'
PS D:\scripts> $t
Fuentes de información
PS D:\scripts>
\(ツ)_/
Friday, November 2, 2018 12:45 AM
Thank you jrv. Should be something on my powershell configuration (win 10 64 bits fresh install), but couldn't figure it out.
Friday, November 2, 2018 1:52 AM
It's weird. This code also works fine for me.
Just do it.
Friday, November 2, 2018 12:07 PM
Where did the original string come from?
Wednesday, November 7, 2018 9:59 AM
Hi,
Was your issue resolved?
If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
Best Regards,
Lee
Just do it.
Wednesday, November 7, 2018 12:40 PM
Hello,
I use this two lines to change document's encoding:
$Content = Get-Content -Path $path
$Out = $Content | Set-Content -path $path -Encoding UTF8
Hope this can help you
Regards,
Wednesday, November 7, 2018 4:30 PM
Powershell 5 has little inconsistencies, like out-file and tee save as unicode, while other commands save as ansi (in the language of notepad). But if it's properly encoded, it should work with most programs. All commands in Powershell 6 save as utf8NoBOM by default (?).
There's a weird issue with get-content and set-content too, where the docs say the default encoding is ascii, and yet accents are preserved by default, and not when specifying the ascii encoding.
Thursday, November 8, 2018 6:23 PM
I can actually reproduce this. If the script is saved as utf8 no bom and run in PS 5, the encoding will not be interpreted correctly. Even the ISE has trouble with it. Utf8 with bom ("signature") works ok.
PS C:\users\admin> .\accent
Fuentes de información
Thursday, November 8, 2018 9:08 PM
You have to specify UTH8 when loading.
$t = Get-Content <filename> -Encoding UTF8
Without a BOM there is no way to select a character set.
\(ツ)_/
Thursday, November 8, 2018 9:21 PM
There's a way to uniquely identify utf8 no bom. https://unicodebook.readthedocs.io/guess_encoding.html In fact it's the default encoding in powershell 6. /en-us/powershell/scripting/whats-new/what-s-new-in-powershell-core-60?view=powershell-6#default-encoding-is-utf-8-without-a-bom-except-for-new-modulemanifest
Tuesday, December 4, 2018 2:28 AM
Hi,
Was your issue resolved?
If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
If no, please reply and tell us the current situation in order to provide further help.
Best Regards,
Lee
Just do it.