business.com receives compensation from some of the companies listed on this page. Advertising Disclosure

Home

Introduction To Using XML Files With PowerShell

Adam Bertram

PowerShell offers several ways to read XML documents, without writing a lot of code.

PowerShell offers several ways to read XML documents, without writing a lot of code or using XPath. Here's how you can get started.

Manipulating XML is one of those topics that makes anyone that's ever done it cringe. It's typically time-consuming, hard to understand and just a general pain in the butt. Messing with XML on some other languages besides PowerShell requires essentially memorizing the XML tree to make sure you're manipulating the right node at the right level. On top of that, you'll probably also have to learn to write obscure XPath queries to find the content in a XML document.

 

PowerShell and Reading XML files

PowerShell still has XPath but you don't necessarily have to use it. PowerShell provides an easy way to read XML files, manipulate them and finally to save them back to disk without writing a lot of code or knowing XPath. PowerShell does this by providing the user dot notation to signify each node in the XML document. But first, let's get started reading an XML document. 

One way to read an XML document in PowerShell is to typecast a variable to the type [xml]. To create this variable, we can use the Get-Content cmdlet to read all of the text in an XML document. To typecast the output of Get-Content we can simply prepend the text [xml] before the variable. This tells PowerShell that we need this variable typecasted as a System.Xml.XmlDocument type instead of the default array type that normally comes from Get-Content.

    [xml]$XmlDocument = Get-Content -Path C:Cars.xml

In this article, we'll be using an XML file called Cars.xml as an example, which contains various makes and models of cars.

Once you've run Get-Content to read the raw text from the XML document and cast the output to type System.Xml.XmlDocument, you now have a variable called $XmlDocument that contains the entire XML node tree that represents that document.

At this point, reading the document is a trivial task due to the aforementioned dot notation. Since we now have the XML document in memory we can explore the tree.

Notice how we can get deeper and deeper into the document using dot notation? If we hit a spot in the document that contains node of like type (Cars) we can then treat it like a PowerShell array and specify an index number of the node we'm looking for. In this case, we're finding the first car in the document by specifying the index number of 0.

Let's say you want to find all of the cars in this XML document that have four or more seats. Simply by using the common PowerShell cmdlet Where-Object we can easily retrieve that information.

Combining Nodes

Notice that PowerShell combines both the nodes (elements) and XML attributes all in one set? This allows you to easily comb through the document just by using dot notation and the Where-Object cmdlet. PowerShell knows all the attributes and sub elements that are associated with each parent XML and displays them in a nice table for you, by default.

We've shown you one method that you can use to read XML documents with. One of the nice things about PowerShell is it can be as easy or as hard as you want it to be. We've shown you how to find nodes in a XML document using dot notation. If you ever have the need to do more advanced searching, we  suggest you look into using cmdlets like Select-Xml to build more powerful queries. Either way, PowerShell will allow you to read and find just about anything from an XML document. The rest is up to you.

 
Image Credit: scyther5/Getty
Adam Bertram
business.com Contributing Writer
Adam Bertram is a 20-year veteran of IT and experienced online business professional. He's an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn, or follow him on Twitter at @adbertram or the TechSnips Twitter account at @techsnips_io.