{"id":3901,"date":"2013-04-01T00:01:00","date_gmt":"2013-04-01T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/04\/01\/working-with-xml\/"},"modified":"2013-04-01T00:01:00","modified_gmt":"2013-04-01T00:01:00","slug":"working-with-xml","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/working-with-xml\/","title":{"rendered":"Working with XML"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Windows PowerShell MVPs, Don Jones, Richard Siddaway, and Jeffrey Hicks share excerpts from their new book<\/span><em style=\"font-size: 12px\">.<\/em><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This week we will not have our usual <strong>PowerTip<\/strong>. Instead we have excerpts from seven books from Manning Press. In addition, each blog will have a special code for 50% off the book being excerpted that day. Remember that the code is valid only for the day the excerpt is posted. The coupon code is also valid for a second book from the Manning collection.<\/p>\n<p>Today, the excerpt is from <a href=\"http:\/\/www.manning.com\/jones2\/\" target=\"_blank\">PowerShell in Depth<\/a><span style=\"text-decoration: underline\"><br \/> <\/span>&nbsp; &nbsp; &nbsp;By Don Jones, Richard Siddaway, and Jeffery Hicks<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7635.hsg-4-1-13-1.jpg\"><img decoding=\"async\" title=\"Photo of book cover\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7635.hsg-4-1-13-1.jpg\" alt=\"Photo of book cover\" \/><\/a><\/p>\n<p>If you use Windows PowerShell, the Help, format, and type files are XML. The new &ldquo;cmdlet over objects&rdquo; functionality in Windows PowerShell 3.0 is based on XML. The HTML- and XML-related functionality hasn&rsquo;t had any major changes in Windows PowerShell 3.0. In this excerpt from&nbsp;<a href=\"http:\/\/www.manning.com\/jones2\/\" target=\"_blank\">PowerShell in Depth<\/a>, the authors cover several capabilities and provide some concise examples of how you might want to use them.<\/p>\n<p>Windows PowerShell includes some great capabilities for working with two common forms of structured data: HTML and XML. Why is this important? Because HTML is a great way to produce professional-looking reports and you can use XML in so many places within your environment. The XML abilities in Windows PowerShell are no less amazing than its HTML abilities. We&rsquo;ll cover a couple of specific use cases and the commands and techniques that help you accomplish each in this tip.<\/p>\n<h2>Using XML to persist data<\/h2>\n<p>One common use of XML is to preserve complex, hierarchical data in a simple, text-based format that&rsquo;s easily transmitted across networks, copied as files, and so forth. XML&rsquo;s other advantage is that it can be read by humans if required. Objects (PowerShell&rsquo;s main form of command output), are one common kind of complex hierarchical data, and a pair of Windows PowerShell cmdlets can help convert objects to and from XML. This process is called serializing (converting objects to XML) and deserializing (converting XML back into objects). It&rsquo;s almost exactly what happens in Windows PowerShell remoting when objects need to be transmitted over the network. Here&rsquo;s a quick example:<span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Process | Export-Clixml proc_baseline.xml<\/p>\n<p>This code creates a static, text-based representation of the processes currently running on the computer. The <strong>Export-Clixml<\/strong> cmdlet produces XML that&rsquo;s specifically designed to be read back in by PowerShell.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong>&nbsp;&nbsp;&nbsp;The <strong>Export<\/strong> verb, unlike the <strong>ConvertTo<\/strong> verb, combines the acts of converting the objects into another data format and writing them to a file.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Import-Clixml .\\proc_baseline.xml | sort -property pm -Descending |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;select -first 10<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Handles&nbsp; NPM(K)&nbsp;&nbsp;&nbsp; PM(K)&nbsp;&nbsp;&nbsp;&nbsp; WS(K) VM(M)&nbsp;&nbsp; CPU(s)&nbsp;&nbsp;&nbsp;&nbsp; Id ProcessName<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;-&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp; &#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211; &#8212;&#8211;&nbsp;&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; &#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 783&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 77&nbsp;&nbsp; 336420&nbsp;&nbsp;&nbsp; 285772&nbsp;&nbsp; 819&nbsp;&nbsp;&nbsp; 43.69&nbsp;&nbsp; 2204 powershell<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 544&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 41&nbsp;&nbsp; 196500&nbsp;&nbsp;&nbsp; 166980&nbsp;&nbsp; 652&nbsp;&nbsp;&nbsp; 13.41&nbsp;&nbsp; 2660 powershell<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 348&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 24&nbsp;&nbsp;&nbsp; 91156&nbsp;&nbsp;&nbsp;&nbsp; 39032&nbsp;&nbsp; 600&nbsp;&nbsp;&nbsp;&nbsp; 1.28&nbsp;&nbsp;&nbsp;&nbsp; 92 wsmprovhost<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 186&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 18&nbsp;&nbsp;&nbsp; 52024&nbsp;&nbsp;&nbsp;&nbsp; 35472&nbsp;&nbsp; 170&nbsp;&nbsp;&nbsp;&nbsp; 5.56&nbsp;&nbsp;&nbsp; 716 dwm<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 329&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 28&nbsp;&nbsp;&nbsp; 24628&nbsp;&nbsp;&nbsp;&nbsp; 24844&nbsp;&nbsp; 213&nbsp;&nbsp;&nbsp;&nbsp; 0.30&nbsp;&nbsp; 2316 iexplore<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 311&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 26&nbsp;&nbsp;&nbsp; 24276&nbsp;&nbsp;&nbsp;&nbsp; 22308&nbsp;&nbsp; 213&nbsp;&nbsp;&nbsp;&nbsp; 0.30&nbsp;&nbsp;&nbsp; 108 iexplore<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 210&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 14&nbsp;&nbsp;&nbsp; 20628&nbsp;&nbsp;&nbsp;&nbsp; 26228&nbsp;&nbsp;&nbsp; 69&nbsp;&nbsp;&nbsp;&nbsp; 5.95&nbsp;&nbsp; 1828 WmiPrvSE<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; 1327&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 41&nbsp;&nbsp;&nbsp; 19608&nbsp;&nbsp;&nbsp;&nbsp; 33164&nbsp;&nbsp; 126&nbsp;&nbsp;&nbsp; 49.45&nbsp;&nbsp;&nbsp; 764 svchost<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 398&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 15&nbsp;&nbsp;&nbsp; 19164&nbsp;&nbsp;&nbsp;&nbsp; 21120&nbsp;&nbsp;&nbsp; 56&nbsp;&nbsp;&nbsp;&nbsp; 3.95&nbsp;&nbsp;&nbsp; 728 svchost<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 722&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 47&nbsp;&nbsp; &nbsp;17992&nbsp;&nbsp;&nbsp;&nbsp; 23080&nbsp; 1394&nbsp;&nbsp;&nbsp; 13.45&nbsp;&nbsp;&nbsp; 924 svchost<\/p>\n<p>The previous example demonstrates that the objects are imported from XML and placed, as objects, into the pipeline, where they can again be sorted, selected, filtered, and so forth. These deserialized objects are static, and their methods have been removed because they&rsquo;re no longer &ldquo;live&rdquo; objects against which actions can be taken.<\/p>\n<p>But because XML captures a hierarchy of object data, it&rsquo;s an excellent tool for capturing complex objects. We recommend using the CliXML format as an intermediary rather than JSON.<\/p>\n<h2>Reading arbitrary XML data<\/h2>\n<p>You might also have a need to work with XML that comes from other sources. For example, the following is a brief XML file that contains information about two computers. You&rsquo;ll use it in a running example:<\/p>\n<p style=\"padding-left: 30px\">&lt;computers&gt;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&lt;computer name=&#8217;WIN8&#8242; \/&gt;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&lt;computer name=&#8217;LOCALHOST&#8217; \/&gt;<\/p>\n<p style=\"padding-left: 30px\">&lt;\/computers&gt;<\/p>\n<p style=\"padding-left: 30px\"><strong>Warning<\/strong>&nbsp;&nbsp;&nbsp;Unlike most of Windows PowerShell, XML tags are case sensitive. Using &lt;computers&gt; and &lt;\/Computers&gt; won&rsquo;t work. Be careful if you&rsquo;re retyping this example to get it exactly as shown here.<\/p>\n<p>The <strong>Get-Content<\/strong> cmdlet can read the plain-text content of this XML file, but it will treat it as plain text. By casting the result as the special [xml] data type, you can force Windows PowerShell to parse the XML into a data structure. You&rsquo;ll store the result in a variable to allow you to work with it and display the contents of the variable:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; [xml]$xml = Get-Content .\\inventory.xml<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $xml<span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\">computers<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;&#8212;<\/p>\n<p style=\"padding-left: 30px\">computers<span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n<p>You can see that the variable contains the top-level XML element, the &lt;computers&gt; tag. That top-level element has become a property. Now you can start exploring the object hierarchy:<span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $xml.computers<\/p>\n<p style=\"padding-left: 30px\">computer<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">{WIN8, LOCALHOST}<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $xml.computers.computer[0]<span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\">name<\/p>\n<p style=\"padding-left: 30px\">&#8212;-<\/p>\n<p style=\"padding-left: 30px\">WIN8<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $xml.computers.computer[1]<\/p>\n<p style=\"padding-left: 30px\">name<\/p>\n<p style=\"padding-left: 30px\">&#8212;-<\/p>\n<p style=\"padding-left: 30px\">LOCALHOST<\/p>\n<p>You can see how it&rsquo;s easy to explore the object model from this point.<\/p>\n<h2>Creating XML data and files<\/h2>\n<p>But reading XML data is only half the fun. Windows PowerShell also lets you create XML files that can be used outside of Windows PowerShell with the <strong>ConvertTo-XML<\/strong> cmdlet:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $xml=Get-WmiObject Win32_Volume | ConvertTo-Xml<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $xml<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">xml&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Objects<\/p>\n<p style=\"padding-left: 30px\">&#8212;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">version=&#8221;1.0&#8243;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Objects<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $xml.objects.object[0]<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Type&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">System.Management.ManagementObject&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {PSComputerName, __GENUS, __CLAS&#8230;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $xml.GetType().Name<\/p>\n<p style=\"padding-left: 30px\">XmlDocument<\/p>\n<p><span style=\"font-size: 12px\">You haven&rsquo;t created an XML file, but only an XML representation of the <\/span><strong style=\"font-size: 12px\">Get-WmiObject<\/strong><span style=\"font-size: 12px\"> result. But assuming you have XML experience and knowledge, you could explore or modify the XML document.<\/span><\/p>\n<p>When you&rsquo;re ready to save the XML document to a file, invoke the <strong>Save()<\/strong> method. You should specify the full path and filename:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $xml.Save(&#8220;c:\\work\\volume.xml&#8221;)<\/p>\n<p><span style=\"font-size: 12px\">Unlike <\/span><strong style=\"font-size: 12px\">ConvertTo-HTML<\/strong><span style=\"font-size: 12px\">, you can&rsquo;t pipe to <\/span><strong style=\"font-size: 12px\">Out-File<\/strong><span style=\"font-size: 12px\"> and expect an XML document. You need to take this manual step, but you can use a shortcut like the following to do it all in one step:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: 12px\">(Get-WmiObject Win32_Volume | ConvertTo-Xml).Save(&#8220;c:\\work\\volume.xml&#8221;)<\/span><\/p>\n<p><span style=\"font-size: 12px\">With so much of the world&rsquo;s data in XML and HTML, being able to work those formats can be handy. Windows PowerShell provides a variety of capabilities that should be able to address most common situations. Obviously, the more knowledge you have of those formats and how they&rsquo;re used, the more effective you&rsquo;ll be with Windows PowerShell&rsquo;s ability to handle them.<\/span><\/p>\n<p><strong>Here is the code for the discount offer today at <\/strong><a href=\"http:\/\/www.manning.com\/\"><strong>www.manning.com<\/strong><\/a>: <strong>scriptw1<\/strong><br \/> Valid for 50% off <a href=\"http:\/\/www.manning.com\/jones2\/\" target=\"_blank\">PowerShell in Depth<\/a> and <a href=\"http:\/\/www.manning.com\/stirk\/\" target=\"_blank\">SQL Server DMVs in Action<br \/><\/a><span style=\"font-size: 12px\">Offer valid from April 1, 2013 12:01 AM until April 2 midnight&nbsp; (EST)<\/span><\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Windows PowerShell MVPs, Don Jones, Richard Siddaway, and Jeffrey Hicks share excerpts from their new book. Microsoft Scripting Guy, Ed Wilson, is here. This week we will not have our usual PowerTip. Instead we have excerpts from seven books from Manning Press. In addition, each blog will have a special code for 50% off [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[64,56,65,189,3,45],"class_list":["post-3901","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-don-jones","tag-guest-blogger","tag-jeffery-hicks","tag-richard-siddaway","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Windows PowerShell MVPs, Don Jones, Richard Siddaway, and Jeffrey Hicks share excerpts from their new book. Microsoft Scripting Guy, Ed Wilson, is here. This week we will not have our usual PowerTip. Instead we have excerpts from seven books from Manning Press. In addition, each blog will have a special code for 50% off [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=3901"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3901\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=3901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}