Parse XML Files Using PowerShell

  1. Parse PowerShell XML Elements
  2. Parse PowerShell XML Attributes
Parse XML Files Using PowerShell

The Extensible Markup Language or XML format is still widely used today. Configuration files, RSS feeds, and Office files are just a few examples that use the XML format.

Using Windows PowerShell to parse XML files is an essential step in our PowerShell journey. This article will discuss how we can parse XML files using Windows PowerShell.

Parse PowerShell XML Elements

First, let us cover one of the most popular and easiest ways to use PowerShell to parse XML with Select-XML. The Select-Xml PowerShell command allows us to supply an XML file or string and an XPath filter to pull out specific information.

We have an XML file on many PCs that we’d like to parse with PowerShell. For example, each machine has a name, an IP address, and an Include element that can be used in a report.

Example XML:

<Computers>
	<Computer>
		<Name>WINPC-01</Name>
		<Ip>127.0.0.1</Ip>
		<Include>true</Include>
	</Computer>
	<Computer>
		<Name>WINPC-02</Name>
		<Ip>192.168.1.105</Ip>
		<Include>false</Include>
	</Computer>
	<Computer>
		<Name>WINPC-03</Name>
		<Ip>192.168.1.104</Ip>
		<Include>true</Include>
	</Computer>
</Computers>

We want to use PowerShell to parse this XML file and get the computer names. To do that, we could use the Select-XML command.

For example, in the file above, the computer names appear in the inner text (InnerXML) of the Name element.

We would first provide the appropriate XPath to find the computer names. Only the Name nodes contained in the Computer elements would be returned using this XPath technique.

Then, using a ForEach-Object loop, reach the Node.InnerXML property on each Name element to get only the InnerXML of that element.

Example Code:

Select-Xml -Path C:\PS\sample.xml -XPath '/Computers/Computer/Name' | ForEach-Object { $_.Node.InnerXML }

Parse PowerShell XML Attributes

Now let’s take a new approach to locating computer names. Instead of being represented by XML elements, computer descriptors are directly expressed by XML attributes.

Below is an example XML file with computer descriptors represented with attributes. We can now see each descriptor as an attribute rather than an element.

Example Code:

<Computers>
	<Computer name="WINPC-01" ip="127.0.0.1" include="true" />
	<Computer name="WINPC-02" ip="192.168.1.104" include="false" />
	<Computer name="WINPC-03" ip="192.168.1.105" include="true" />
</Computers>

Since each descriptor is an attribute, tweak the XPath only to find the Computer elements. Then, using a ForEach-Object cmdlet, find the name attribute’s value.

Example Code:

Select-Xml -Path C:\Work\computers-attr.xml -XPath '/Computers/Computer' | ForEach-Object { $_.Node.name }

Output:

WINPC-01
WINPC-02
WINPC-03

Whether we are reading elements or attributes, the syntax of Select-Xml is cumbersome: it forces us to use the XPath parameter, then pipe the result to a loop, and finally look for the data under the Node property.

Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

Related Article - PowerShell XML