DevOps | Scripts | Automation

PowershellXML

How to change XML encoding from Utf8-BOM to UTF8 using PowerShell?

Why do we need to convert to XML format?

  • XML file format UTF8-BOM doesn’t recognize by all applications and needs to be converted to UTF8 format.
  • Many times (not all time), some XML files when you parse XML with PowerShell and use the XML save() function then it automatically converts the file to UTF8-BOM format.

Let’s first check how the file is automatically converted to BOM format when you use the Save() function in PowerShell.

Example,

For this example, we are using a sample XML file from the Microsoft website. If you open that file in Notepad++ then you can see the encoding is UTF-8, let’s convert it to UTF8-BOM format with Notepad++ only.

Now when you check its encoding it’s in BOM format.

Let’s convert back it to UTF-8 format with PowerShell only. Basically, UTF-8 is the default format for XML and if we remove the current encoding then it will be automatically converted to UTF-8 format.

Use the below command for it,

$xmlFile = [xml](Get-Content C:\Temp\SampleXML.xml) 
$xmlFile.ChildNodes[0].Encoding = $null
$xmlFile.Save("C:\Temp\SampleXML.xml")

When you check the encoding now, it will be converted to UTF-8 format.

Loading