EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell XML Parsing
 

PowerShell XML Parsing

Updated March 6, 2023

PowerShell XML Parsing.

 

 

Introduction to PowerShell XML Parsing

The following article provides an outline for PowerShell XML Parsing. The XML parsing is the library or the process to connect to your XML document and read the document’s content, filtering out specific XML documents, navigating to the nodes, and validating the XML document. PowerShell uses the Select-XML command line to parse the document. In turn, it uses the XPath (XML path) method and another method called the .Net method to parse the XML document for ease of XML document search and readability.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax of PowerShell XML Parsing

Given below are the syntax mentioned:

Select-Xml
[-Xml] <XmlNode[]>
[-XPath] <String>
[-Namespace <Hashtable>] [<CommonParameters>]

Select-Xml
[-Path] <String[]>
[-XPath] <String>
[-Namespace <Hashtable>] [<CommonParameters>]

Select-Xml
-LiteralPath <String[]>
[-XPath] <String>
[-Namespace <Hashtable>] [<CommonParameters>]

Select-Xml
-Content <String[]>
[-XPath] <String>
[-Namespace <Hashtable>] [<CommonParameters>]

The above 4 different mentions that only one pair can be used at a time. Like we can not use the -LiteralPath and -Content parameter at the same time. They can be used in the same combination as mentioned.

How to Perform XML Parsing in PowerShell?

As we know, XML parsing is analyzing the XML documents, syntax, and navigating nodes. XML uses Select-XML command line or .Net method for it.

Suppose we have a sample XML file shown below:

Part of the sample XML file has been taken from the MS website.

https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms762271(v=vs.85)

Code:

<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
</catalog>

To work with the XML content, we first need to content to be passed in XML format. If we use the simple Get-Content command, it won’t be in actual XML format but the raw content.

Code:

Get-Content C:\Temp\books.xml

Output:

PowerShell XML Parsing 1

To work with the Select-XML method or DotNet method, we need to convert the raw file into XML format.

There are two ways to convert a file into XML.

  • Type Conversion

$xmldoc = [xml](Get-Content C:\Temp\books.xml)

  • With Add-Type Method

$xml = New-Object -TypeName XML
$xml.Load('C:\Temp\books.xml')

When we use the Select-XML method, XPath is mandatory.

To select the root node (catalog) using XPath.

Code:

Select-Xml -Xml $xml -XPath "/catalog"

Output:

using XPath

The other way to navigate is using the DotNet method.

Code:

$xml.catalog
book
----
{book, book, book}

Examples of PowerShell XML Parsing

Given below are the examples mentioned:

Example #1

Expand the root node.

With Select-XML method to expand the root or the first parent node (catalog).

Code:

$xmldoc = [xml](Get-Content C:\Temp\books.xml)
Select-Xml -Xml $xmldoc -XPath "/catalog"

Output:

PowerShell XML Parsing 3

Once we get the above output, we need to expand the Node property.

Code:

Select-Xml -Xml $xmldoc -XPath "/catalog" | Select -ExpandProperty Node

Output:

PowerShell XML Parsing 4

Example #2

Using the SelectNodes method.

Instead of using the Select-XML method, we can use the SelectNodes method to search from the XML file.

Code:

$xmldoc = [xml](Get-Content C:\Temp\books.xml)
$xmldoc.SelectNodes("/catalog")

Output:

SelectNodes

In the above example, ‘/’ mentions the select from the root node. At the root node, we have only the ‘Catalog’ node available. If we use the ‘Book’ node instead, it won’t give any output because it is not at the root level.

PowerShell XML Parsing 6

When we use the ‘//’ syntax in the xPath method, it selects that node from the entire document no matter where they are.

Code:

PS C:\> $xmldoc.SelectNodes("//catalog")

Output:

PowerShell XML Parsing 7

This will search for all the catalog nodes.

The above command is similar to.

Code:

Select-Xml -Xml $xmldoc -XPath "//catalog" | Select -ExpandProperty Node

And

Code:

$xmldoc.catalog

Output:

PowerShell XML Parsing 8

To select all the books from the XML file.

Code:

$xmldoc.SelectNodes("//book")

Output:

select all the books

Similar commands

Code:

Select-Xml -Xml $xmldoc -XPath "//book" | Select -ExpandProperty Node

And

Code:

$xmldoc.catalog.book

To select all the authors from the file.

Code:

$xmldoc.SelectNodes("//author")

Output:

PowerShell XML Parsing 10

Similar commands

Code:

Select-Xml -Xml $xmldoc -XPath "//author" | Select -ExpandProperty Node

And

Code:

$xmldoc.catalog.Book.author

Output:

PowerShell XML Parsing 11

Example #3

Using SelectSingleNode method.

SelectSingleNode method is to get the particular node output. By default, this command selects the first output from the array if the index is not mentioned and the index starts from 1 in XML.

Code:

$xmldoc.SelectSingleNode('//book')

Output:

SelectSingleNode

This command is similar to,

Code:

$xmldoc.SelectSingleNode('//book[1]')

Or

Select-Xml -Xml $xmldoc -XPath "//book[1]" | Select -ExpandProperty Node

Or

$xmldoc.catalog.book[1]

  • To select another node:

Suppose we want to select the second book author then, we can use below example.

Code:

$xmldoc.SelectSingleNode("//book[2]/author")
Select-Xml -Xml $xmldoc -XPath "//book[2]/author"

Output:

second book author

Alternate commands

Code:

$xmldoc.catalog.book[2].author

Or

Select-Xml -Xml $xmldoc -XPath "//book[2]/author" | Select -ExpandProperty Node

Output:

PowerShell XML Parsing 14

Example #4

Selecting a particular attribute.

In this example, we will select a particular attribute. We need to select here the node which has book id: bk103.

Code:

$xmldoc.SelectNodes( "//book[@id='bk103']" )

Output:

Selecting a particular attribute

You can also use the alternate commands for Select-XML and dotnet method, as shown below.

Code:

Select-Xml -Xml $xmldoc -XPath("//book[@id='bk103']") | Select -ExpandProperty Node

Or

$xmldoc.catalog.book | where {$_.id -eq 'bk103'}

Output:

PowerShell XML Parsing 16

Example #5

Selecting multiple elements.

To select multiple elements, we can use the below command.

Code:

$xmldoc.SelectNodes("//book/title | //book/author")

Or

$xmldoc.SelectNodes("//title | //author")

The above command selects the title and author from the XML file.

Output:

Selecting multiple elements

Similar commands

Code:

Select-Xml -Xml $xmldoc -XPath ("//title | //author") | Select -ExpandProperty Node

$xmldoc.catalog.book | Select Title, Author

Output:

PowerShell XML Parsing 18

Example #6

Single and Double dot syntax Xpath.

In the XPath syntax, Dot (.) represents the current node while double dot (..) represents the parent of the current node.

Code:

$xmldoc.SelectSingleNode("//catalog/.")

Output:

PowerShell XML Parsing 19

The above example represents the current node.

Code:

$xmldoc.SelectSingleNode("//catalog/..")

Output:

PowerShell XML Parsing 20

The above example represents the parent node.

Conclusion

PowerShell XML parsing methods are useful while working with the XML documents, so once it is used in the application, it can be easier for applications to search and navigate the larger documents quickly, and also it is very fast for web processing.

Recommended Articles

This is a guide to PowerShell XML Parsing. Here we discuss the introduction, how to perform XML parsing in PowerShell? and examples respectively. You may also have a look at the following articles to learn more –

  1. PowerShell Sleep
  2. PowerShell SubString
  3. PowerShell not like
  4. Else If in PowerShell

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW