Section: Powershell With Xml

Query XML Data with Powershell


Introduction

XML files and data can be over encumbering and confusing to read. With the help of PowerShell this tutorial is going to help you learn how to query XML data and save valuable time. The syntax being used in today's tutorial is the XPath Syntax. This is the standard syntax used by PowerShell when performing queries against XML data.

The Commands

The interesting thing about XPath is that while it isstill it isown language by itself, it ismodeled after a common directory structure we see in our modern PC architecture. Below we can see an example of an XPath query string:

Example

$path = "/note/heading"

Copy and Try it

Let us save some XML data to an XML variable to test our XPath string:

Example

$xml = [XML] "
User1
User2
Reminder
Hello World
"

Copy and Try it

we can focus only on the value of the property used using the $path and writing XPath path query:

Example

Select-Xml -XPath $query $xml | Select -Expand Node

Copy and Try it

As metioned, we are parsing the XML and then defines which item we would like to access from the original data. The Select -Expand The Node Statement is important for this example because it returns the node (the value) of the heading property. Other Example on XML with PowershellQuery xml data powershell