Alright, I’m trying to piecemeal instructions that I’ve found online, and I’m struggling… I have an XML file that is saved as UTF8. My objective is to create a backup (which I’m having no problem doing, edit two of the values, and save it. Every time I save it ends up as UTF8-BOM. I’m lost and hoping someone can help me… Anyone able to point to what I’m obviously doing wrong?
$DateStamp = get-date -uformat "%Y_%m_%d_%H.%M.%S"
$File = "$PathFile.xml"
Copy-Item $File -Destination "$PathFile.xml.$DateStamp.bak"
[xml]$XMLFile = GC $File
$AuditEvent = $XMLFile.File.Job | Where-object Name -EQ 'AuditEvent'
$LogEvent = $XMLFile.File.Job | Where-object Name -EQ 'LogEvent'
$AuditEvent.ExpiryDuration = "$Audit"
$LogEvent.ExpiryDuration = "$Log"
$XMLFile.Save($File)
[System.Xml.XmlWriterSettings] $XmlSettings = New-Object System.Xml.XmlWriterSettings
$XmlSettings.Indent = $false
$XmlSettings.Encoding = New-Object System.Text.UTF8Encoding($false)
[System.Xml.XmlWriter] $XmlWriter = [System.Xml.XmlWriter]::Create($XMLFile, $XmlSettings)
$XMLFile.Save($XmlWriter)
$XmlWriter.Dispose()