{"id":6811,"date":"2008-01-18T11:27:00","date_gmt":"2008-01-18T11:27:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2008\/01\/18\/format-xml\/"},"modified":"2019-02-18T13:16:17","modified_gmt":"2019-02-18T20:16:17","slug":"format-xml","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/format-xml\/","title":{"rendered":"Format-XML"},"content":{"rendered":"<p>Have you ever had an XML file that looks like crap?<\/p>\n<p>The problem is that XML is sorta the data encoding equivalent of <a href=\"http:\/\/snltranscripts.jt.org\/75\/75ishimmer.phtml\">Shimmer<\/a> &#8211; sometimes its an encoding for programs and sometimes its an encoding for users.<\/p>\n<p>Our PowerShell MVP Brandon Shell pointed out that the output of Export-CLIXML looked like cra&#8230;.&nbsp; errrrrr &#8230;. well let&#8217;s just say that was &#8220;programmer oriented&#8221;.&nbsp; \ud83d\ude42&nbsp; <\/p>\n<p>Its true, we were thinking about programs consuming our XML not users.&nbsp; You&#8217;ll find lots of XML like that in the world (one reason is that it saves a ton of space by eliminating whitespaces).<\/p>\n<p>So if you ever have a file like that and want to be able to read it, you&#8217;ll want FORMAT-XML:<\/p>\n<p><font face=\"Courier New\" size=\"2\">function Format-XML ([xml]$xml, $indent=2) <br \/>{ <br \/>&nbsp;&nbsp;&nbsp; $StringWriter = New-Object System.IO.StringWriter <br \/>&nbsp;&nbsp;&nbsp; $XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter <br \/>&nbsp;&nbsp;&nbsp; $xmlWriter.Formatting = &#8220;indented&#8221; <br \/>&nbsp;&nbsp;&nbsp; $xmlWriter.Indentation = $Indent <br \/>&nbsp;&nbsp;&nbsp; $xml.WriteContentTo($XmlWriter) <br \/>&nbsp;&nbsp;&nbsp; $XmlWriter.Flush() <br \/>&nbsp;&nbsp;&nbsp; $StringWriter.Flush() <br \/>&nbsp;&nbsp;&nbsp; Write-Output $StringWriter.ToString() <br \/>}<\/font><\/p>\n<p>Use it like this:<\/p>\n<p>PS&gt; Format-XML ([xml](cat c:\\ps\\r_and_j.xml)) -indent 4<\/p>\n<p>Enjoy!<\/p>\n<p><strong>&nbsp;UPDATE:&nbsp; <\/strong><\/p>\n<p>Lee Holmes points out that page 153 of his <a class=\"\" href=\"http:\/\/www.oreilly.com\/catalog\/9780596528492\/\">CookBook<\/a> has an even simpler&nbsp;example:<\/p>\n<p class=\"MsoNormal\"><span>gps powershell | Export-CliXml c:\\temp\\powershell.xml<\/span><\/p>\n<p class=\"MsoNormal\"><span>$xml = [xml] (gc c:\\temp\\powershell.xml)<\/span><\/p>\n<p class=\"MsoNormal\"><span>$xml.Save([Console]::Out)<\/span><\/p>\n<p><p>Nicely done Lee!<\/p>\n<p>Jeffrey Snover [MSFT] <br \/>Windows Management Partner Architect <br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a> <br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever had an XML file that looks like crap? The problem is that XML is sorta the data encoding equivalent of Shimmer &#8211; 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&#8230;.&nbsp; errrrrr &#8230;. [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6811","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>Have you ever had an XML file that looks like crap? The problem is that XML is sorta the data encoding equivalent of Shimmer &#8211; 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&#8230;.&nbsp; errrrrr &#8230;. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6811","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=6811"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6811\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=6811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=6811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=6811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}