Have you ever had an XML file that looks like crap?

The problem is that XML is sorta the data encoding equivalent of Shimmer – sometimes its an encoding for programs and sometimes its an encoding for users.

Our PowerShell MVP Brandon Shell pointed out that the output of Export-CLIXML looked like cra….  errrrrr …. well let’s just say that was “programmer oriented”.  🙂 

Its true, we were thinking about programs consuming our XML not users.  You’ll find lots of XML like that in the world (one reason is that it saves a ton of space by eliminating whitespaces).

So if you ever have a file like that and want to be able to read it, you’ll want FORMAT-XML:

function Format-XML ($xml, $indent=2)
    $StringWriter = New-Object System.IO.StringWriter
    $XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter
    $xmlWriter.Formatting = “indented”
    $xmlWriter.Indentation = $Indent
    Write-Output $StringWriter.ToString()

Use it like this:

PS> Format-XML ((cat c:\ps\r_and_j.xml)) -indent 4



Lee Holmes points out that page 153 of his CookBook has an even simpler example:

gps powershell | Export-CliXml c:\temp\powershell.xml

$xml = (gc c:\temp\powershell.xml)


Nicely done Lee!

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at:
Visit the Windows PowerShell ScriptCenter at: