1

By operating with a file in UTF8 without BOM

$xml = New-Object xml
$xml.PreserveWhitespace = $true
$xml.Load($path)
...
$node.InnerText = '©' + (Get-Date).year
...
$utf8WithoutBom = New-Object System.Text.UTF8Encoding($false)
$sw = New-Object System.IO.StreamWriter($path, $false, $utf8WithoutBom)
$xml.Save($sw)
$sw.Close()

Result is shown incorrectly in the file

<copyright>©2022</copyright>

No effect as well by using:

$OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding

In addition, untouched fields are preserved with copyright and other symbols.

FieryCat
  • 1,875
  • 18
  • 28
  • 1
    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 [this answer](https://stackoverflow.com/a/54790355/45375) for more information. – mklement0 Feb 02 '22 at 23:16
  • 1
    Thanks @mklement0, post your answer to close the question, you've saved my day ) – FieryCat Feb 02 '22 at 23:26
  • 1
    Glad to hear it helped, FieryCat, but, given your feedback, it's better to close your question as a duplicate of the linked post, as I have just done. But I invite you to up-vote the accepted answer to the linked post. – mklement0 Feb 02 '22 at 23:33

0 Answers0