EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell XML Parsing
Secondary Sidebar
PowerShell Tutorial
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell XML Parsing

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.

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>]

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,527 ratings)

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://docs.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
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more