Importing Complex XML into PowerShell

Posted on January 13, 2017 by Jeff Hicks in PowerShell with 0 Comments

We’re almost finished with our exploration of using XML and PowerShell. Hopefully, you’ve been following along from the start and I hope you’ve been experimenting with your own XML files. In the previous article, I shared some techniques for bringing XML files back into PowerShell that you might have created using ConvertTo-XML. But you might have customized such a file, as I did in a previous article in this series when I created an inventory XML file.

My inventory XML file

My inventory XML file (Image Credit: Jeff Hicks)

The challenge is that there are several different type of objects described here and there is no explicit type information for anything. In order to import this into PowerShell as an object, you really have to know the XML file structure.



As before, the first step is to convert the file into an XML document.

The imported document

The imported document (Image Credit: Jeff Hicks)

I know that all of the data is in the Computers node. But how you import the data is completely up to you.

Here’s one scenario that “imports” the XML data as a complex object.

Viewing all converted data

Viewing all converted data (Image Credit: Jeff Hicks)

Each of the child nodes in the XML file is a separate object.

Using converted data

Using converted data (Image Credit: Jeff HIcks)

It is beyond the scope of this article, but I could also insert a custom typename and then use custom type and format files. Or I might import the XML file with a different set of commands.

Perhaps I want the object structured a bit differently.

In this example, I’ve also inserted a new typename for my custom object. I did this because I defined a default set of display properties using Update-TypeData.

By default this gives me the view I want.

A customized view

A customized view (Image Credit: Jeff Hicks)

Of course, all of the properties are still accessible.

All properties

All properties (Image Credit: Jeff Hicks)

The bottom line is that no matter how you bring the data into PowerShell, you want to have objects so that you can use the results like anything else in PowerShell.

Data as objects

Data as objects (Image Credit: Jeff Hicks)

These techniques should work for any type of XML file, even something that isn’t Windows or computer specific.¬† Here’s my BandData.xml file.

I want to bring this into PowerShell as a set of objects.

Again, I can’t stress enough that you really need to know in advance how your XML file is structured. But now I have a set of objects I can work with.

Using other types of data

Using other types of data (Image Credit: Jeff Hicks)


I think this example should bring us full circle in this series. I didn’t intend this to be an exhaustive series, although it may have tired you out in spots. For example, at some point I should probably give you some exposure to use XPath and finding data inside an XML file. But I’ll leave that for the future. I expect you have had your fill of XML for awhile, so next time we’ll play with something else.


Tagged with , ,