{"id":15961,"date":"2011-01-09T00:01:00","date_gmt":"2011-01-09T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/01\/09\/weekend-scripter-easily-explore-xml-files-with-powershell\/"},"modified":"2011-01-09T00:01:00","modified_gmt":"2011-01-09T00:01:00","slug":"weekend-scripter-easily-explore-xml-files-with-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-easily-explore-xml-files-with-powershell\/","title":{"rendered":"Weekend Scripter: Easily Explore XML Files with PowerShell"},"content":{"rendered":"<p><span style=\"font-size: 10pt\">&nbsp;<\/span><span style=\"font-size: 10pt\">&nbsp;<\/span>\n<b><span style=\"font-size: 10pt\">Summary:<\/span><\/b><span style=\"font-size: 10pt\"> Guest blogger and Microsoft MVP, Sean Kearney, shows how to explore XML files with Windows PowerShell in this beginner how-to article.<\/span>\n<span style=\"font-size: 10pt\">&nbsp;<\/span>\n<span style=\"font-size: 10pt\">Microsoft Scripting Guy Ed Wilson here. We have another guest blogger this weekend. Sean Kearney has been working away on his keyboard and is so forthcoming in sharing his work. You can read about Sean in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/12\/19\/weekend-scripter-holiday-powershell-carol-part-one.aspx\"><span style=\"color: #0000ff\">the introduction of his holiday special blog<\/span><\/a>. I am always excited to receive an email from Sean with the subject of &ldquo;I have been playing again&hellip;&rdquo;<\/span><\/p>\n<p style=\"margin: 0in 0in 8pt 0.5in\"><b><span style=\"font-size: 10pt\">Note:<\/span><\/b><span style=\"font-size: 10pt\"> Today&rsquo;s post is about working with XML from within Windows PowerShell. I have written <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/scripting+techniques\/xml\/\"><span style=\"color: black;text-decoration: none\"><span style=\"font-family: Times New Roman\">a series of posts about XML<\/span><\/span><\/a>, and you may wish to continue learning about XML by reading them. <\/span>\n<span style=\"font-size: 10pt\">Sometimes I get to play.&nbsp; I like to play.&nbsp; Today is <a href=\"http:\/\/en.wikipedia.org\/wiki\/Boxing_Day\"><span style=\"color: #0000ff\">Boxing Day<\/span><\/a>, and I&rsquo;m sheepishly in the house playing with Windows PowerShell.<\/span>\n<span style=\"font-size: 10pt\">Until this minute, I really had no clue why anybody cared about XML.&nbsp; Today I do,&nbsp; and BOY, is it freaking cool!<\/span>\n<span style=\"font-size: 10pt\">XML stands for &ldquo;eXtensible Markup Language&rdquo;&nbsp;but that is about all we are going to touch on the politics or descriptions of whys and wherefores and <a href=\"http:\/\/dictionary.reference.com\/browse\/Whatnot&amp;\"><span style=\"color: #0000ff\">whatnots<\/span><\/a>.<\/span>\n<span style=\"font-size: 10pt\">Just think of it this way. XML can store Rows and Columns (like a simple database) but in a straight text file.<\/span>\n<span style=\"font-size: 10pt\">But if you look at an XML file as an ITPro, you might get a headache translating this to something useful.<\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 0.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">&lt;ListOfData&gt;<br \/>&nbsp; &lt;Table&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;Country&gt;United States&lt;\/Country&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;City&gt;Ansonia&lt;\/City&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;StateProvince&gt;CT&lt;\/StateProvince&gt;<br \/>&nbsp; &lt;\/Table&gt;<br \/>&nbsp; &lt;Table&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;Country&gt;Canada&lt;\/Country&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;City&gt;Toronto&lt;\/City&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;StateProvince&gt;ON&lt;\/StateProvince&gt;<br \/>&nbsp; &lt;\/Table&gt;<br \/>&nbsp; &lt;Table&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;Country&gt;Canada&lt;\/Country&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;City&gt;Calgary&lt;\/City&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;StateProvince&gt;AB&lt;\/StateProvince&gt;<br \/>&nbsp; &lt;\/Table&gt;<br \/>&nbsp; &lt;Table&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;Country&gt;United States&lt;\/Country&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;City&gt;Los Angeles&lt;\/City&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;StateProvince&gt;CA&lt;\/StateProvince&gt;<br \/>&nbsp; &lt;\/Table&gt;<br \/>&lt;\/ListOfData&gt;<\/span><\/span>\n<span style=\"font-size: 10pt\">&nbsp;<\/span>\n<span style=\"font-size: 10pt\">If you read it, sure you could decode that in your head. However, imagine a more complex XML document with far more information that just a simple City, Country and Province. Do you really want to be parsing code&nbsp;in your head? I do not. This is where Windows PowerShell steps in. Let us imagine this data is stored in a text file called <b>Locations.txt<\/b><\/span><span style=\"font-size: x-small\">.<span style=\"font-size: 10pt\"> This could be something used by a simple piece of software.&nbsp;&nbsp; <\/span><\/span>\n<span style=\"font-size: 10pt\">Let us just say we want to view this in something more useful, such as something that would Translate XML to information that would make more sense to us.<\/span>\n<span style=\"font-size: 10pt\">I just need to get that information and store it in what is called a &ldquo;TYPED&rdquo; variable. Yes, it is a developer word but do not run off. All it means is that normally Windows PowerShell takes information and pretty accurately guesses what it is, and how it should be stored in a variable. XML is&nbsp;a little bit sneaky. It is text, but no, it&rsquo;s not text, but it&rsquo;s a table, but no it&rsquo;s not a table &ndash; you get the idea. <\/span>\n<span style=\"font-size: 10pt\">So normally, I would do something like this if I were playing with some data in a text file<\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 0.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">$DATA=GET-CONTENT C:Locations.txt<\/span><\/span>\n<span style=\"font-size: 10pt\">&nbsp;<\/span>\n<span style=\"font-size: 10pt\">Afterwards I could play about with <b>$DATA<\/b>, viewing examining various bits. However, if I did that with the data above us (That XML goody) it would be, sub-optimal to say the least. <\/span>\n<span style=\"font-size: 10pt\">With XML I do this first.<\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 0.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">[xml]$DATA=GET-CONTENT C:Locations.txt<\/span><\/span>\n<span style=\"font-size: 10pt\">&nbsp;<\/span>\n<span style=\"font-size: 10pt\">What this does is says, &ldquo;HEY Windows PowerShell, anything that&rsquo;s going into here is coming from an XML formatted text file! So hop off that chair and get busy.&rdquo;<\/span>\n<span style=\"font-size: 10pt\">When we are done with <b>Locations.txt<\/b>, instead of <b>$DATA<\/b> just mimicking the text, it breaks it into, well almost a spreadsheet setup. To find out what we have available we would type:<\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 0.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">$DATA<\/span><\/span>\n<span style=\"font-size: 10pt\">&nbsp;<\/span>\n<span style=\"font-size: 10pt\">Which would give us this result:<\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 0.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">ListOfData<br \/>&#8212;&#8212;&#8212;-<br \/>ListOfData<\/span><\/span>\n<span style=\"font-size: 10pt\">&nbsp;<\/span>\n<span style=\"font-size: 10pt\">But accessing the property called <b>ListOfData<\/b> like so:<\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 0.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">$DATA.ListofData<\/span><\/span>\n<span style=\"font-size: 10pt\">&nbsp;<\/span>\n<span style=\"font-size: 10pt\">Shows us more goodies, as seen below.<\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 0.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">Table<br \/>&#8212;&#8211;<br \/>{Table, Table, Table, Table}<\/span><\/span>\n<span style=\"font-size: 10pt\">&nbsp;<\/span>\n<span style=\"font-size: 10pt\">Digging, digging, digging deeper we key in the following command:<\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 0.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">$DATA.ListofData.Table<\/span><\/span>\n<span style=\"font-size: 10pt\">&nbsp;<\/span>\n<span style=\"font-size: 10pt\">Finally, we actually get to see the XML&nbsp; data translated into something far more useful to us.<\/span><\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=\"MsoNormalTable\" style=\"margin: auto auto auto 0.5in;width: 441px\">\n<tbody>\n<tr>\n<td width=\"128\" valign=\"top\" style=\"background-color: transparent;width: 96.15pt;border: #f0f0f0;padding: 1.5pt\">\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 38.9pt\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">Country<\/span><\/span><\/p>\n<\/td>\n<td width=\"112\" valign=\"top\" style=\"background-color: transparent;width: 84.1pt;border: #f0f0f0;padding: 1.5pt\">\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 38.9pt\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">City<\/span><\/span><\/p>\n<\/td>\n<td width=\"201\" valign=\"top\" style=\"background-color: transparent;width: 150.4pt;border: #f0f0f0;padding: 1.5pt\">\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 38.9pt\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">StateProvince<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"128\" valign=\"top\" style=\"background-color: transparent;width: 96.15pt;border: #f0f0f0;padding: 1.5pt\">\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 38.9pt\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">&#8212;&#8212;&#8212;<\/span><\/span><\/p>\n<\/td>\n<td width=\"112\" valign=\"top\" style=\"background-color: transparent;width: 84.1pt;border: #f0f0f0;padding: 1.5pt\">\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 38.9pt\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">&#8212;&#8211;<\/span><\/span><\/p>\n<\/td>\n<td width=\"201\" valign=\"top\" style=\"background-color: transparent;width: 150.4pt;border: #f0f0f0;padding: 1.5pt\">\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 38.9pt\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"128\" valign=\"top\" style=\"background-color: transparent;width: 96.15pt;border: #f0f0f0;padding: 1.5pt\">\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 38.9pt\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">United States<\/span><\/span><\/p>\n<\/td>\n<td width=\"112\" valign=\"top\" style=\"background-color: transparent;width: 84.1pt;border: #f0f0f0;padding: 1.5pt\">\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 38.9pt\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">Ansonia<\/span><\/span><\/p>\n<\/td>\n<td width=\"201\" valign=\"top\" style=\"background-color: transparent;width: 150.4pt;border: #f0f0f0;padding: 1.5pt\">\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 38.9pt\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">CT<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"128\" valign=\"top\" style=\"background-color: transparent;width: 96.15pt;border: #f0f0f0;padding: 1.5pt\">\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 38.9pt\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">Canada<\/span><\/span><\/p>\n<\/td>\n<td width=\"112\" valign=\"top\" style=\"background-color: transparent;width: 84.1pt;border: #f0f0f0;padding: 1.5pt\">\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 38.9pt\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">Toronto<\/span><\/span><\/p>\n<\/td>\n<td width=\"201\" valign=\"top\" style=\"background-color: transparent;width: 150.4pt;border: #f0f0f0;padding: 1.5pt\">\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 38.9pt\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">ON<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"128\" valign=\"top\" style=\"background-color: transparent;width: 96.15pt;border: #f0f0f0;padding: 1.5pt\">\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 38.9pt\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">Canada<\/span><\/span><\/p>\n<\/td>\n<td width=\"112\" valign=\"top\" style=\"background-color: transparent;width: 84.1pt;border: #f0f0f0;padding: 1.5pt\">\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 38.9pt\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">Calgary<\/span><\/span><\/p>\n<\/td>\n<td width=\"201\" valign=\"top\" style=\"background-color: transparent;width: 150.4pt;border: #f0f0f0;padding: 1.5pt\">\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 38.9pt\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">AB<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"128\" valign=\"top\" style=\"background-color: transparent;width: 96.15pt;border: #f0f0f0;padding: 1.5pt\">\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 38.9pt\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">United States<\/span><\/span><\/p>\n<\/td>\n<td width=\"112\" valign=\"top\" style=\"background-color: transparent;width: 84.1pt;border: #f0f0f0;padding: 1.5pt\">\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 38.9pt\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">Los Angeles<\/span><\/span><\/p>\n<\/td>\n<td width=\"201\" valign=\"top\" style=\"background-color: transparent;width: 150.4pt;border: #f0f0f0;padding: 1.5pt\">\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 38.9pt\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">CA<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-size: 10pt\">&nbsp;<\/span>\n<span style=\"font-size: 10pt\">At this point, you can access the data just as a normal list of data in Windows PowerShell and quickly search through it. Imagine this was a list of tens of thousands. Once in XML format, we can search or manipulate like normal in Windows PowerShell. You can even convert that data to a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Comma-separated_values\"><span style=\"color: #0000ff\">CSV<\/span><\/a> file if you need to do so. That is what the following command does. <\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 0.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">$DATA.ListofData.Table | Where { $_.City&nbsp;&ndash;like &lsquo;*Calgary*&rsquo; }<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt 0.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">$DATA.ListofData.Table | EXPORT-CSV C:locations.csv<\/span><\/span>\n<span style=\"font-size: 10pt\">&nbsp;<\/span>\n<span style=\"font-size: 10pt\">There is really a lot you can do with XML data in this manner including adding new content easily. However, today, we are stopping with just showing you the one thing I learned today. As I said, I thought it was pretty cool.<\/span>\n<span style=\"font-size: 10pt\">&nbsp;<\/span>\n<span style=\"font-size: 10pt\">Thanks Sean for your willingness to share your time and knowledge. Join me tomorrow as I begin a series of articles that talks about handling input for your script. <\/span>\n<span style=\"font-size: 10pt\">I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\"><span style=\"color: #0000ff\">Twitter<\/span><\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><span style=\"color: #0000ff\">Facebook<\/span><\/a>. If you have any questions, send email to me at <a target=\"_blank\" href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\"><span style=\"color: #0000ff\">scripter@microsoft.com<\/span><\/a> or post them on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\"><span style=\"color: #0000ff\">Official Scripting Guys Forum<\/span><\/a>. See you tomorrow. Until then, peace.<\/span>\n<span style=\"font-size: 10pt\">&nbsp;<\/span>\n<b><span style=\"font-size: 10pt\">Ed Wilson, Microsoft Scripting Guy<\/span><\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;&nbsp; Summary: Guest blogger and Microsoft MVP, Sean Kearney, shows how to explore XML files with Windows PowerShell in this beginner how-to article. &nbsp; Microsoft Scripting Guy Ed Wilson here. We have another guest blogger this weekend. Sean Kearney has been working away on his keyboard and is so forthcoming in sharing his work. You [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,56,3,4,154,61,45,165],"class_list":["post-15961","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-guest-blogger","tag-scripting-guy","tag-scripting-techniques","tag-sean-kearney","tag-weekend-scripter","tag-windows-powershell","tag-xml"],"acf":[],"blog_post_summary":"<p>&nbsp;&nbsp; Summary: Guest blogger and Microsoft MVP, Sean Kearney, shows how to explore XML files with Windows PowerShell in this beginner how-to article. &nbsp; Microsoft Scripting Guy Ed Wilson here. We have another guest blogger this weekend. Sean Kearney has been working away on his keyboard and is so forthcoming in sharing his work. You [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15961","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=15961"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15961\/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=15961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=15961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=15961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}