endianness<\/a> of the encoding.<\/p>\n\n\n\nHow to save in UTF-8 without BOM<\/h2>\n\n\n\n
In a nutshell what you need to set false on the attribute with:<\/p>\n\n\n\n
New-Object System.Text.UTF8Encoding($false)<\/code><\/pre>\n\n\n\nAfter that, it’s a matter to save the encoding setting for the file. Here’s 2 examples XML and text file as a bonus the text file example manipulate JSON file.<\/p>\n\n\n\n
XML Edition<\/h3>\n\n\n\n[XML] $XmlDocument = ( Select-Xml -Path $FilePath -XPath \/ ).Node\n\n[System.Xml.XmlWriterSettings] $XmlSettings = New-Object System.Xml.XmlWriterSettings\n\n#Preserve Windows formating\n$XmlSettings.Indent = $true\n\n#Keeping UTF-8 without BOM\n$XmlSettings.Encoding = New-Object System.Text.UTF8Encoding($false)\n\n[System.Xml.XmlWriter] $XmlWriter = [System.Xml.XmlWriter]::Create($FilePath, $XmlSettings)\n$XmlDocument.Save($XmlWriter)\n\n#Close Handle and flush\n$XmlWriter.Dispose()<\/code><\/pre>\n\n\n\nText file Edition<\/h3>\n\n\n\n
Here’s is a JSON example.<\/p>\n\n\n\n
$FilePath = "C:\\MyFolder\\textfile.json"\n$FileContent = Get-Content -Path $FilePath | ConvertFrom-Json\n\n$UTF8Only = New-Object System.Text.UTF8Encoding($false)\n[System.IO.File]::WriteAllLines($FilePath, @($FileContent | ConvertTo-Json), $UTF8Only)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"When you save something as UTF-8 in PowerShell, it, most likely, will be as UTF-8 with BOM. This can be…<\/p>\n","protected":false},"author":5614970,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[178495,672890843],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.n-dol.org\/wp-json\/wp\/v2\/posts\/724"}],"collection":[{"href":"https:\/\/blog.n-dol.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.n-dol.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.n-dol.org\/wp-json\/wp\/v2\/users\/5614970"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.n-dol.org\/wp-json\/wp\/v2\/comments?post=724"}],"version-history":[{"count":20,"href":"https:\/\/blog.n-dol.org\/wp-json\/wp\/v2\/posts\/724\/revisions"}],"predecessor-version":[{"id":1411,"href":"https:\/\/blog.n-dol.org\/wp-json\/wp\/v2\/posts\/724\/revisions\/1411"}],"wp:attachment":[{"href":"https:\/\/blog.n-dol.org\/wp-json\/wp\/v2\/media?parent=724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.n-dol.org\/wp-json\/wp\/v2\/categories?post=724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.n-dol.org\/wp-json\/wp\/v2\/tags?post=724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}