16
votes

Importing any valid XML file as source using [XML]$Var = Get-Content -Path $PathToAnyValidXML I am unable to export it's content properly.

Using Set-Content SomePath $Var , the file ends with System.Xml.XmlDocument as content.

Using $Var | Export-Clixml SomePath , the file ends with the original XML inside an XML

What is a correct method to write raw XML to a file properly ? (Get the exact same content that was on $PathToAnyValidXML once it was imported to a variable on Powershell)

If we need to use XPath or Objects, provide example with both method if possible.

3

3 Answers

24
votes

Since you imported the file as an XML Object using [XML], to export it that way you should use the Save() Mathod.

$VAR.save("c:\works.xml")

I hope this helps

4
votes

I'm using PowerShell 5.1 with PowerShell Extensions (Pscx) installed, and a convenient way that I found is to do this:

$Var | Format-Xml | Out-File $file_path
-3
votes

Give `Out-File a try and see if that does what you're after.

$Var | Out-File SomePath