powershellcharacter-encodingsymbolsemoji

How to embed Windows symbols (Win+;) into the code, to be printed on file and on screen?


On Windows when you press the Windows key and the semicolon button ";" you can chose for some colored symbol (e.g. 🔸) and use it on documents, notepad, posts on social: anything.

Now, I want to embed these symbols into Powershell so that they can be printed on screen

Write-Host "🔸important🔸"

Or saved on text files

On VSC I can insert the symbols as they are and they are correctly represented into the code

$Footer = "`n*CONCEPTS*`n🔸`n`n*KEY MOMENTS*`n🔸"

But when the file is generated, I just get "??" instead of the symbols.

Here's my PowerShell version:

enter image description here

NOTE: I searched in the internet but I didn't find anything about this. perhaps I don't use the correct terminology to identify and isolate this case.


Solution

  • The indication given by @mathias R. Jessen about the switch -encode is paramount

    Indeed by adding the switch -encode utf8 it correctly applies the symbol pasted into VSC as well.

    So it's not necessary to have instructions like this

    $orangeDiamond = [char]::ConvertFromUtf32(0x1F538)
    

    In my foreach() cycle where the file txt is built line-by-line, just by adding the suggested switch, it worked out perfectly

    Add-Content -Path $OutputFile -Value "$mainStringOutput" -Encoding UTF8
    

    This is valid for both: Powershell 5 and 7.

    In my case $mainStringOutput is a string built into the foreach() cycle in this way

        $mainStringOutput= "🔸  $a. $leftPart - $rightPart"
    

    The text file is automatically saved as UTF-8 with BOM in order for Windows PowerShell (v5.1) to read it correctly it means that it contains the required text and the correct format for the so called "Emoji" ("Symbols" in English).

    By reopening it with NOtepad, Notepad++, VSC, the Emojis are correctly displayed.

    No need to do of anything else. Top!