Weekend Scripter: Easily Explore XML Files with PowerShell

ScriptingGuy1

   Summary: Guest blogger and Microsoft MVP, Sean Kearney, shows how to explore XML files with Windows PowerShell in this beginner how-to article.   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 the introduction of his holiday special blog. I am always excited to receive an email from Sean with the subject of “I have been playing again…”

Note: Today’s post is about working with XML from within Windows PowerShell. I have written a series of posts about XML, and you may wish to continue learning about XML by reading them. Sometimes I get to play.  I like to play.  Today is Boxing Day, and I’m sheepishly in the house playing with Windows PowerShell. Until this minute, I really had no clue why anybody cared about XML.  Today I do,  and BOY, is it freaking cool! XML stands for “eXtensible Markup Language” but that is about all we are going to touch on the politics or descriptions of whys and wherefores and whatnots. Just think of it this way. XML can store Rows and Columns (like a simple database) but in a straight text file. But if you look at an XML file as an ITPro, you might get a headache translating this to something useful.

<ListOfData>
  <Table>
    <Country>United States</Country>
    <City>Ansonia</City>
    <StateProvince>CT</StateProvince>
  </Table>
  <Table>
    <Country>Canada</Country>
    <City>Toronto</City>
    <StateProvince>ON</StateProvince>
  </Table>
  <Table>
    <Country>Canada</Country>
    <City>Calgary</City>
    <StateProvince>AB</StateProvince>
  </Table>
  <Table>
    <Country>United States</Country>
    <City>Los Angeles</City>
    <StateProvince>CA</StateProvince>
  </Table>
</ListOfData>
  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 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 Locations.txt. This could be something used by a simple piece of software.   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. I just need to get that information and store it in what is called a “TYPED” 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 a little bit sneaky. It is text, but no, it’s not text, but it’s a table, but no it’s not a table – you get the idea. So normally, I would do something like this if I were playing with some data in a text file

$DATA=GET-CONTENT C:Locations.txt   Afterwards I could play about with $DATA, 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. With XML I do this first.

[xml]$DATA=GET-CONTENT C:Locations.txt   What this does is says, “HEY Windows PowerShell, anything that’s going into here is coming from an XML formatted text file! So hop off that chair and get busy.” When we are done with Locations.txt, instead of $DATA just mimicking the text, it breaks it into, well almost a spreadsheet setup. To find out what we have available we would type:

$DATA   Which would give us this result:

ListOfData
———-
ListOfData
  But accessing the property called ListOfData like so:

$DATA.ListofData   Shows us more goodies, as seen below.

Table
—–
{Table, Table, Table, Table}
  Digging, digging, digging deeper we key in the following command:

$DATA.ListofData.Table   Finally, we actually get to see the XML  data translated into something far more useful to us.

Country

City

StateProvince

———

—–

——————

United States

Ansonia

CT

Canada

Toronto

ON

Canada

Calgary

AB

United States

Los Angeles

CA

  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 CSV file if you need to do so. That is what the following command does.

$DATA.ListofData.Table | Where { $_.City –like ‘*Calgary*’ }

$DATA.ListofData.Table | EXPORT-CSV C:locations.csv   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.   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. I invite you to follow me on Twitter or Facebook. If you have any questions, send email to me at scripter@microsoft.com or post them on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.   Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon