30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
SharePoint Diary
Salaudeen Rajack's Experiences on SharePoint,
PowerShell, Microsoft 365 and related products!
SharePoint Diary » PowerShell » XML Manipulation in PowerShell: A Comprehensive Guide
PowerShell PowerShell Tutorials XML
XML Manipulation in PowerShell: A
Comprehensive Guide
Updated: November 16, 2024 Salaudeen Rajack
Introduction to XML manipulation in
PowerShell
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 1/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
XML, or Extensible Markup Language, is a widely used data format that is
human-readable and machine-readable. It is commonly used for data
exchange, configuration management, and document representation.
XML with PowerShell can save the day. Maybe you need to export to XML,
import from XML, extract specific information, modify certain elements, or
even create new XML files from scratch.
In this article, we will explore how to use PowerShell to work with XML
files, from creating and reading to parsing and transforming. Whether
you’re a PowerShell pro or just starting out, this article will give you the
knowledge to become an XML processing master.
📜 Table of contents
Introduction to XML manipulation in PowerShell
Understanding XML structure and syntax
How to create XML files using PowerShell?
Reading XML files in PowerShell
Importing XML content in PowerShell using Get-Content
Parsing and querying XML in PowerShell
Looping Through XML Nodes Using PowerShell
Extracting node values from XML in PowerShell
Adding Nodes and Attributes to XML Elements with
PowerShell
PowerShell to Remove a Node in XML
Changing XML values with PowerShell
Converting XML to other formats (CSV, JSON) using
PowerShell
Convert XML to CSV using PowerShell
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 2/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
Convert XML to JSON using PowerShell
Converting CSV files to XML with PowerShell
Convert Custom Objects (PSCustomObject) to XML File
Tips and Best Practices for Working with XML in PowerShell
Conclusion
Understanding the Basics of XML
XML is a widely adopted markup language for storing and transmitting
structured data. It’s a standard way to represent information in many
applications, from config files to data exchange protocols. Understanding
the basics of XML is the first step to using it in your PowerShell scripts.
XML documents are made up of elements surrounded by opening and
closing tags. These elements can have attributes, text content, and even
nested sub-elements, creating a hierarchical structure that mirrors the
data. Being able to navigate and manipulate this XML structure is key to
data processing in PowerShell.
Understanding XML structure and syntax
Before we discuss the specifics of working with XML in PowerShell, it’s
important to have a solid understanding of the XML structure and syntax.
XML documents are composed of elements, which are the building blocks
of the data. Each element can have attributes that provide additional
information and contain text content or other nested elements.
Here’s an example of a simple XML document for a Book entity:
Copy
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 3/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
1 <?xml version="1.0" encoding="UTF-8"?>
2 <book>
3 <title>The Great Gatsby</title>
4 <author>FScott Fitzgerald</author>
5 <publication_year>1925</publication_year>
6 <genre>Fiction</genre>
7 </book>
In this example, the <book> element is the root element, and it contains
four child elements: <title> , <author> , <publication_year> , and
<genre> . Each of these child elements has a specific piece of information
about the book.
Understanding the structure and syntax of XML is crucial for effectively
working with XML data in PowerShell.
Important: XML is case sensitive language! So, make sure you use
the correct case when handing XML in PowerShell!
How to create XML files using PowerShell?
Creating XML files in PowerShell is a straightforward process. You can use
the [System.Xml.XmlDocument] class to create a new XML document
and add elements, attributes, and text content.
Here’s an example of how to create a simple XML file using PowerShell.
This basic script serves as a foundation. From here, you can expand and
customize it to suit your needs, such as adding attributes, more nested
elements, or processing existing XML files.
Copy
1 # Create a new XML document
2 $xml = [System.Xml.XmlDocument]::new()
3
4 # Add the XML declaration
5 $declaration = $xml.CreateXmlDeclaration("1.0", "UTF-8", $null)
6 $xml.AppendChild($declaration) | Out-Null
7
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 4/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
8 # Create the root element
9 $rootElement = $xml.CreateElement("book")
10 $xml.AppendChild($rootElement) | Out-Null
11
12 # Add child elements
13 $titleElement = $xml.CreateElement("title")
14 $titleElement.InnerText = "The Great Gatsby"
15 $rootElement.AppendChild($titleElement) | Out-Null
16
17 $authorElement = $xml.CreateElement("author")
18 $authorElement.InnerText = "F. Scott Fitzgerald"
19 $rootElement.AppendChild($authorElement) | Out-Null
20
21 $publicationYearElement = $xml.CreateElement("publication_year")
22 $publicationYearElement.InnerText = "1925"
23 $rootElement.AppendChild($publicationYearElement) | Out-Null
24
25 $genreElement = $xml.CreateElement("genre")
26 $genreElement.InnerText = "Fiction"
27 $rootElement.AppendChild($genreElement) | Out-Null
28
29 # Save the XML document to a file
30 $xml.Save("C:\Data\book.xml")
In this example, we create a new [System.Xml.XmlDocument] object, add
the XML declaration, create the root element, and then add child
elements with their respective text content. Finally, we save the XML
document to a file named “book.xml”.
This is just one way to create XML files using PowerShell. You can also use
the [System.Xml.XmlWriter] class or the ConvertTo-Xml cmdlet to
achieve similar results.
Outputting PowerShell Data to XML Files
In addition to the above method, PowerShell also allows you to create
and export PowerShell object data to XML files. This can be particularly
useful when generating custom XML documents or updating existing
ones programmatically.
To create a new XML file, you can use the ConvertTo-Xml Cmdlet:
Copy
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 5/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
1 # Get Data
2 $Services = Get-service | Select-Object Name, DisplayName, Statu
3
4 #Convert to XML
5 $xmlData = $Services | ConvertTo-Xml -NoTypeInformation
6
7 #Save to a File
8 $xmlData.Save("C:\Data\Services.xml")
This code creates a new XML document with a root element and saves it
to the specified file path.
Reading XML files in PowerShell
Reading XML files in PowerShell is also a straightforward process. You can
use the [System.Xml.XmlDocument] class to load an XML file into
memory and then navigate and manipulate the data as needed.
Here’s an example of how to read a specific node in an XML file using
PowerShell:
Copy
1 # Load the XML file into a [System.Xml.XmlDocument] object
2 $xml = [System.Xml.XmlDocument]::new()
3 $xml.Load("C:\Data\book.xml")
4
5 # Access the root element
6 $rootElement = $xml.DocumentElement
7
8 # Access child elements
9 $title = $rootElement.SelectSingleNode("title").InnerText
10 $author = $rootElement.SelectSingleNode("author").InnerText
11 $publicationYear = $rootElement.SelectSingleNode("publication_ye
12 $genre = $rootElement.SelectSingleNode("genre").InnerText
13
14 # Print the values
15 Write-Host "Title: $title"
16 Write-Host "Author: $author"
17 Write-Host "Publication Year: $publicationYear"
18 Write-Host "Genre: $genre"
In this example, we first load the “book.xml” file into a
[System.Xml.XmlDocument] object using the Load() method. We then
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 6/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
access the root element using the DocumentElement property, and then
use the SelectSingleNode() method to select and retrieve the values of
the child elements.
Finally, we print the values of the various elements to the console.
Importing XML content in PowerShell using Get-
Content
Let’s take the example of Book XML, and here is the PowerShell script to
read the XML file and access its content using Get-Content cmdlet:
Copy
1 # Read the XML file content
2 $xmlContent = [xml](Get-Content -Path "C:\Data\book.xml")
3
4 # Get XML nodes
5 Write-host "Title:" $xmlDocument.Book.Title
6 Write-host "Author:" $xmlDocument.Book.Year
7 Write-host "Year:" $xmlDocument.Book.Year
8 Write-host "ISBN:"$xmlDocument.Book.ISBN
9 Write-host "Publisher:"$xmlDocument.Book.Publisher
This is just one way to read XML files using PowerShell.
Parsing and querying XML in PowerShell
Once an XML document is loaded into memory, you can use various
methods and properties to parse and query the data. PowerShell provides
several built-in methods and properties that make navigating and
manipulating XML data easy. Here is my Books.xml file with an array of
elements.
Copy
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 7/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
1 <?xml version="1.0" encoding="UTF-8"?>
2 <Books>
3 <Book>
4 <Title>PowerShell for Beginners</Title>
5 <Author>Jane Smith</Author>
6 <Year>2023</Year>
7 <ISBN>123-4567890123</ISBN>
8 <Publisher>Tech Books Publishing</Publisher>
9 </Book>
10 <Book>
11 <Title>Advanced PowerShell</Title>
12 <Author>John Doe</Author>
13 <Year>2022</Year>
14 <ISBN>789-1234567890</ISBN>
15 <Publisher>Pro Books Publishing</Publisher>
16 </Book>
17 </Books>
Here’s how to parse and query an XML document using PowerShell:
In this script, we first load the “books.xml” file into a
[System.Xml.XmlDocument] object. We then used the method to get all
the elements in the document, and then iterated through them to print
the title and author of each book.
Copy
1 # Load the XML file into a [System.Xml.XmlDocument] object
2 $xml = [System.Xml.XmlDocument]::new()
3 $xml.Load("C:\Data\books.xml")
4
5 # Get all the book elements
6 $books = $xml.SelectNodes("//Book")
7
8 # Iterate through the books and print their titles and authors
9 foreach ($book in $books) {
10 $title = $book.SelectSingleNode("Title").InnerText
11 $author = $book.SelectSingleNode("Author").InnerText
12 Write-Host "Title: $title, Author: $author"
13 }
14
15 # Get the publication year of the first book
16 $publicationYear = $books[0].SelectSingleNode("Year").InnerText
17 Write-Host "Publication Year: $publicationYear"
18
19 # Get the genre of the first book
20 $Publisher = $books[0].SelectSingleNode("Publisher").InnerText
21 Write-Host "Publisher: $Publisher"
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 8/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
This script also demonstrates how to access specific elements, such as the
publication year and genre, using the SelectSingleNode() method.
PowerShell provides a variety of methods and properties for querying and
navigating XML data, including SelectNodes() , SelectSingleNode() ,
GetElementsByTagName() , and more. By mastering these techniques, you
can efficiently extract the information you need from complex XML
documents.
Looping Through XML Nodes Using PowerShell
One of the most common tasks when working with XML data is traversing
the hierarchical structure to access specific nodes or elements. PowerShell
provides several methods to accomplish this, including the use of
ForEach-Object and Select-Xml cmdlets.
Here is the PowerShell method to iterate through XML nodes:
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 9/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
Copy
1 $xmlData = [xml](Get-Content -Path "C:\Temp\book.xml")
2
3 $xmlData.Book.ChildNodes | ForEach-Object {
4 Write-Host $_.Name
5 }
To get the value of each node, use:
Copy
1 # Step 1: Read the XML file content
2 $xmlContent = [xml](Get-Content -Path "C:\Data\books.xml")
3
4 # Step 2: Access XML nodes
5 foreach ($book in $xmlContent.Books.Book) {
6 $title = $book.Title
7 $author = $book.Author
8 $year = $book.Year
9 $isbn = $book.ISBN
10 $publisher = $book.Publisher
11
12 # Print the values
13 Write-Output "Title: $title"
14 Write-Output "Author: $author"
15 Write-Output "Year: $year"
16 Write-Output "ISBN: $isbn"
17 Write-Output "Publisher: $publisher"
18 Write-Output "-------------------------"
19 }
You can also navigate through the XML and parse using XPath: The
Select-Xml cmdlet in PowerShell is a powerful tool for targeting specific
nodes or elements within an XML document. By leveraging XPath
expressions, you can precisely locate and extract the data you need.
Copy
1 # Use XPath expression to select elements
2 $elements = Select-Xml -Path "C:\Data\books.xml" -XPath "//Book"
3
4 # Loop through the selected elements
5 foreach ($element in $elements) {
6 Write-Host "Title: $($element.Node.title)"
7 Write-Host "Author: $($element.Node.author)"
8 Write-host
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 10/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
9 }
Extracting node values from XML in PowerShell
Extracting the values of XML elements is a common task in PowerShell.
You can use the InnerText property of an XmlElement object to retrieve
the text content of an element.
Here’s an example to extract from an array of elements:
Copy
1 # Load the XML file into a [System.Xml.XmlDocument] object
2 $xml = [System.Xml.XmlDocument]::new()
3 $xml.Load("C:\Data\books.xml")
4
5 # Get the title of the first book
6 $title = $xml.SelectSingleNode("//book[1]/title").InnerText
7 Write-Host "Title: $title"
8
9 # Get the author of the first book
10 $author = $xml.SelectSingleNode("//book[1]/author").InnerText
11 Write-Host "Author: $author"
In this example, we first load the “books.xml” file into a
[System.Xml.XmlDocument] object. We then use the
SelectSingleNode() method to get the <title> and <author>
elements of the first <book> element, and then use the InnerText
property to retrieve their values.
Adding Nodes and Attributes to XML
Elements with PowerShell
Adding a node to an existing XML file using PowerShell involves loading
the XML document, creating a new node, appending it to the appropriate
location, and then saving the modified XML document. Here’s a step-by-
step guide to do this:
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 11/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
Here’s an example of how to add a new element to an XML document
using PowerShell:
Copy
1 # Load the XML file into a [System.Xml.XmlDocument] object
2 $xml = [System.Xml.XmlDocument]::new()
3 $xml.Load("C:\Data\book.xml")
4
5 # Get the root element
6 $rootElement = $xml.DocumentElement
7
8 # Create a new element
9 $newElement = $xml.CreateElement("ISBN")
10 $newElement.InnerText = "123-4567890123"
11
12 # Append the new element to the root element
13 $rootElement.AppendChild($newElement) | Out-Null
14
15 # Save the updated XML document
16 $xml.Save("C:\Data\book.xml")
In this example, we first load the “book.xml” file into a
[System.Xml.XmlDocument] object. We then use the CreateElement()
method to create a new <ISBN> element and set its text content to “123-
4567890123”. Finally, we append the new element to the root element
using the AppendChild() method and save the updated XML document.
How about adding a nested element?
Copy
1 # Load the XML file
2 $xmlDocument = [xml](Get-Content -Path "C:\Data\book.xml")
3
4 # Create a new nested element
5 $reviews = $xmlDocument.CreateElement("Reviews")
6
7 $review1 = $xmlDocument.CreateElement("Review")
8 $review1.SetAttribute("Reviewer", "Alice")
9 $review1.InnerText = "Great introduction to PowerShell!"
10 $reviews.AppendChild($review1)
11
12 $review2 = $xmlDocument.CreateElement("Review")
13 $review2.SetAttribute("Reviewer", "Bob")
14 $review2.InnerText = "Advanced concepts explained well."
15 $reviews.AppendChild($review2)
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 12/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
16
17 # Append the nested element to the first Book element
18 $firstBook = $xmlDocument.SelectSingleNode("//book")
19 $firstBook.AppendChild($reviews)
20
21 # Save the updated XML document
22 $xmlDocument.Save("C:\Data\book.xml")
You can also use PowerShell to add new attributes to existing elements.
Here’s an example:
Copy
1 # Load the XML file into a [System.Xml.XmlDocument] object
2 $xml = [System.Xml.XmlDocument]::new()
3 $xml.Load("C:\Data\book.xml")
4
5 # Get the root element
6 $rootElement = $xml.DocumentElement
7
8 # Create a new attribute
9 $newAttribute = $xml.CreateAttribute("edition")
10 $newAttribute.Value = "1st"
11
12 # Add the attribute to the root element
13 $rootElement.Attributes.Append($newAttribute) | Out-Null
14
15 # Save the updated XML document
16 $xml.Save("C:\Data\book.xml")
In this example, we create a new attribute called “edition” with a value of
“1st”, and then add it to the root element using the CreateAttribute()
and Attributes.Append() methods.
Here is the script in action with a new attribute, element, and nested
elements:
Copy
1 <?xml version="1.0" encoding="UTF-8"?>
2 <book edition="1st">
3 <title>The Great Gatsby</title>
4 <author>FScott Fitzgerald</author>
5 <publication_year>1926</publication_year>
6 <genre>Fiction</genre>
7 <ISBN>123-4567890123</ISBN>
8 <Reviews>
9 <Review Reviewer="Alice">Great introduction to PowerShell!</
10 <Review Reviewer="Bob">Advanced concepts explained well.</Re
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 13/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
11 </Reviews>
12 </book>
By mastering these techniques, you can easily modify and expand the
data stored in your XML documents using PowerShell.
PowerShell to Remove a Node in XML
Let’s remove the genre node in Book.
Copy
1 # Load the XML file
2 $xmlContent = Get-Content -Path "C:\Data\book.xml"
3 [xml]$xmlDocument = $xmlContent
4
5 # Remove the genre node
6 $genre = $xmlDocument.SelectSingleNode("//genre")
7 $genre.ParentNode.RemoveChild($genre)
8
9 # Save the updated XML document
10 $xmlDocument.Save("C:\Data\book_updated.xml")
PowerShell provides a powerful way to automate and manipulate XML
data when working with XML files. Using the above methods, you can
easily read, update, add, or remove nodes and attributes within your XML
files.
Changing XML values with PowerShell
In addition to adding new elements and nodes, you can also use
PowerShell to change the values of existing elements in an XML
document. This can be useful when you need to update specific pieces of
information in your XML data.
Here’s how to change the value of an existing element in an XML
document using PowerShell:
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 14/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
Copy
1 # Load the XML file into a [System.Xml.XmlDocument] object
2 $xml = [System.Xml.XmlDocument]::new()
3 $xml.Load("C:\Data\book.xml")
4
5 # Get the root element
6 $rootElement = $xml.DocumentElement
7
8 # Change the publication year
9 $publicationYearElement = $rootElement.SelectSingleNode("publica
10 $publicationYearElement.InnerText = "1926"
11
12 # Save the updated XML document
13 $xml.Save("C:\Data\book.xml")
In this example, we first load the “book.xml” file into a
[System.Xml.XmlDocument] object. We then use the
SelectSingleNode() method to get the <publication_year> element,
and then change its InnerText property to “1926”. Finally, we save the
updated XML document.
Tips: In case, your XML document has multiple elements, You can
update a specific node like:
$firstBookTitle =
$xmlDocument.SelectSingleNode("//Book[1]/Title")
$firstBookTitle.InnerText = "PowerShell Basics"
Similar techniques can be used to change the values of other elements or
attributes in your XML data. By combining these methods with the
parsing and querying techniques we covered earlier, you can create
powerful scripts that can automate a wide range of XML-related tasks.
Let’s update the edition attribute of the book element:
Copy
1 # Load the XML file
2 $xmlContent = Get-Content -Path "C:\Data\book.xml"
3 [xml]$xmlDocument = $xmlContent
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 15/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
4
5 # Update the edition attribute
6 $book = $xmlDocument.SelectSingleNode("//book")
7 $book.SetAttribute("edition", "2nd")
8
9 # Save the updated XML document
10 $xmlDocument.Save("C:\Data\book.xml")
Converting XML to other formats (CSV,
JSON) using PowerShell
One of the powerful features of PowerShell is its ability to convert XML
data to other commonly used data formats, such as CSV and JSON. This
can be particularly useful when you need to integrate your XML data with
other systems or applications that may not natively support XML.
Convert XML to CSV using PowerShell
Here’s an example of how to convert an XML document to a CSV file
using PowerShell:
Copy
1 # Load the XML file into a [System.Xml.XmlDocument] object
2 $xml = [System.Xml.XmlDocument]::new()
3 $xml.Load("C:\Data\book.xml")
4
5 # Get all the book elements
6 $books = $xml.SelectNodes("//book")
7
8 # Create a list to hold the book data
9 $bookData = [System.Collections.Generic.List[PSCustomObject]]::n
10
11 # Iterate through the books and add their data to the list
12 foreach ($book in $books) {
13 $bookData.Add([PSCustomObject]@{
14 Title = $book.SelectSingleNode("title").InnerText
15 Author = $book.SelectSingleNode("author").InnerText
16 "Publication Year" = $book.SelectSingleNode("publication_
17 Genre = $book.SelectSingleNode("genre").InnerText
18 })
19 }
20
21 # Export the data to a CSV file
22 $bookData | Export-Csv -Path "C:\Data\book_data.csv" -NoTypeInfo
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 16/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
In this example, we first load the “book.xml” file into a
[System.Xml.XmlDocument] object. We then use a foreach loop to
iterate through the <book> elements and create a list of
PSCustomObject objects, each representing a book and containing its
title, author, publication year, and genre.
Finally, we use the Export-Csv cmdlet to save the book data to a CSV file
named “book_data.csv”.
Alternatively, You can use the below PowerShell script which extracts all
nodes and attributes and exports to CSV:
Copy
1 # Load the XML file
2 $xmlContent = Get-Content -Path "C:\Data\book.xml"
3 [xml]$xmlDocument = $xmlContent
4
5 # Extract the main book information
6 $bookInfo = [PSCustomObject]@{
7 Title = $xmlDocument.book.title
8 Author = $xmlDocument.book.author
9 Publication_Year = $xmlDocument.book.publication_year
10 Genre = $xmlDocument.book.genre
11 ISBN = $xmlDocument.book.ISBN
12 Edition = $xmlDocument.book.edition
13 }
14
15 # Extract the reviews information
16 $reviews = @()
17 foreach ($review in $xmlDocument.book.Reviews.Review) {
18 $reviewInfo = [PSCustomObject]@{
19 Reviewer = $review.Reviewer
20 Review = $review.InnerText
21 }
22 $reviews += $reviewInfo
23 }
24
25 # Save the main book information to a CSV file
26 $bookInfo | Export-Csv -Path "C:\Data\book_info.csv" -NoTypeInfo
27
28 # Save the reviews information to a CSV file
29 $reviews | Export-Csv -Path "C:\Data\book_reviews.csv" -NoTypeIn
As the source XML Book.XML has nested elements, we end up exporting
to two different CSVs. What if your XML has an array of elements, Say:
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 17/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
Books.xml?
Copy
1 # Load the XML file
2 $xmlContent = Get-Content -Path "C:\Data\books.xml"
3 [xml]$xmlDocument = $xmlContent
4
5 # Extract the book information
6 $books = @()
7 foreach ($book in $xmlDocument.Books.Book) {
8 $bookInfo = [PSCustomObject]@{
9 Title = $book.Title
10 Author = $book.Author
11 Year = $book.Year
12 ISBN = $book.ISBN
13 Publisher = $book.Publisher
14 }
15 $books += $bookInfo
16 }
17
18 # Save the book information to a CSV file
19 $books | Export-Csv -Path "C:\Data\books.csv" -NoTypeInformation
Convert XML to JSON using PowerShell
You can also use PowerShell to convert XML data to JSON format. Here’s
an example XML:
Copy
1 <?xml version="1.0" encoding="UTF-8"?>
2 <Book>
3 <Title>PowerShell for Beginners</Title>
4 <Author>Jane Smith</Author>
5 <Year>2023</Year>
6 <ISBN>123-4567890123</ISBN>
7 <Publisher>Tech Books Publishing</Publisher>
8 </Book>
And the PowerShell Script to convert the above XML to JSON:
Copy
1 # Load the XML file
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 18/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
2 $xmlContent = Get-Content -Path "C:\Data\book.xml"
3 [xml]$xmlDocument = $xmlContent
4
5 # Extract the book element
6 $book = $xmlDocument.Book
7
8 # Create a custom object to hold the book information
9 $bookInfo = [PSCustomObject]@{
10 Title = $book.Title
11 Author = $book.Author
12 Year = $book.Year
13 ISBN = $book.ISBN
14 Publisher = $book.Publisher
15 }
16
17 # Convert the custom object to JSON
18 $jsonContent = $bookInfo | ConvertTo-Json
19
20 # Save the JSON content to a file
21 $jsonContent | Out-File "C:\Data\book.json"
In this example, we use the ConvertTo-Json cmdlet to convert the XML
document to a JSON string, and then use the Out-File cmdlet to save
the JSON data to a file named “book.json”.
If you are converting an XML that has an array of elements (E.g.,
Books.xml in our case), use the following script:
Copy
1 # Load the XML file
2 $xmlContent = Get-Content -Path "C:\Data\books.xml"
3 [xml]$xmlDocument = $xmlContent
4
5 # Extract the book information
6 $books = @()
7 foreach ($book in $xmlDocument.Books.Book) {
8 $bookInfo = [PSCustomObject]@{
9 Title = $book.Title
10 Author = $book.Author
11 Year = $book.Year
12 ISBN = $book.ISBN
13 Publisher = $book.Publisher
14 }
15 $books += $bookInfo
16 }
17
18 # Save the book information to a CSV file
19 $books | ConvertTo-Json | Set-Content -Path "C:\Data\books.json
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 19/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
By leveraging these conversion capabilities, you can easily integrate your
XML data with other systems and applications, making it more accessible
and usable in various contexts.
Converting CSV files to XML with
PowerShell
PowerShell also allows you to convert CSV files to XML format. This can
be useful when you integrate data from a CSV file with an XML-based
system or application.
Here’s an example of how to convert a CSV file to XML using PowerShell:
Copy
1 # Step 1: Read the CSV file
2 $csvPath = "C:\Data\books-data.csv"
3 $csvContent = Import-Csv -Path $csvPath
4
5 # Step 2: Create a new XML document
6 $xmlDocument = New-Object System.Xml.XmlDocument
7
8 # Step 3: Add the XML declaration
9 $declaration = $xmlDocument.CreateXmlDeclaration("1.0", "UTF-8",
10 $xmlDocument.AppendChild($declaration) | Out-Null
11
12 # Step 4: Create the root element
13 $root = $xmlDocument.CreateElement("Books")
14 $xmlDocument.AppendChild($root)
15
16 # Step 5: Loop through each row in the CSV and create XML elemen
17 foreach ($row in $csvContent) {
18 $bookElement = $xmlDocument.CreateElement("Book")
19 $root.AppendChild($bookElement)
20
21 foreach ($column in $row.PSObject.Properties) {
22 $element = $xmlDocument.CreateElement($column.Name)
23 $element.InnerText = $column.Value
24 $bookElement.AppendChild($element)
25 }
26 }
27
28 # Step 6: Save the XML document to a file
29 $xmlPath = "C:\Data\books.xml"
30 $xmlDocument.Save($xmlPath)
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 20/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
In this example, we first use the Import-Csv cmdlet to import the “book-
data.csv” file into a PowerShell object. We then create a new
[System.Xml.XmlDocument] object and add the XML declaration. We
create a root <books> element and then iterate through the CSV data,
creating a <book> element for each row and adding the corresponding
title, author, publication year, and genre elements.
Finally, we save the XML document to a file named “books.xml”. With the
powerful combination of PowerShell and XML, you can streamline your
workflows, automate repetitive tasks, and gain valuable insights from
your data.
Convert Custom Objects (PSCustomObject)
to XML File
Converting custom objects (PSCustomObject) to XML in PowerShell is a
straightforward process. Here’s how you can do it:
Copy
1 # Step 1: Create a PSCustomObject
2 $book = [PSCustomObject]@{
3 Title = "PowerShell for Beginners"
4 Author = "Jane Smith"
5 Year = 2023
6 ISBN = "123-4567890123"
7 Publisher = "Tech Books Publishing"
8 }
9
10 # Step 2: Create a new XML document
11 $xmlDocument = New-Object System.Xml.XmlDocument
12
13 # Step 3: Create the root element
14 $root = $xmlDocument.CreateElement("Book")
15 $xmlDocument.AppendChild($root)
16
17 # Step 4: Add child elements for each property of the PSCustomOb
18 $book.PSObject.Properties | ForEach-Object {
19 $element = $xmlDocument.CreateElement($_.Name)
20 $element.InnerText = $_.Value
21 $root.AppendChild($element)
22 }
23
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 21/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
24 # Step 5: Save the XML document to a file
25 $xmlDocument.Save("C:\Data\bookV4.xml")
Output:
Copy
1 <Book>
2 <Title>PowerShell for Beginners</Title>
3 <Author>Jane Smith</Author>
4 <Year>2023</Year>
5 <ISBN>123-4567890123</ISBN>
6 <Publisher>Tech Books Publishing</Publisher>
7 </Book>
To convert a custom object (PSCustomObject) to an XML file in
PowerShell, you can use the ConvertTo-Xml cmdlet. Here’s an example:
Copy
1 # Create a PSCustomObject
2 $book = [PSCustomObject]@{
3 Title = "PowerShell for Beginners"
4 Author = "Jane Smith"
5 Year = 2023
6 ISBN = "123-4567890123"
7 Publisher = "Tech Books Publishing"
8 }
9
10 # Convert the PSCustomObject to XML
11 $xmlString = $book | ConvertTo-Xml -As String
12
13 $xmlString | Out-File -FilePath "C:\Data\BookV2.xml" -Encoding U
Output:
Copy
1 <?xml version="1.0" encoding="utf-8"?>
2 <Objects>
3 <Object Type="System.Management.Automation.PSCustomObject">
4 <Property Name="Title" Type="System.String">PowerShell for B
5 <Property Name="Author" Type="System.String">Jane Smith</Pro
6 <Property Name="Year" Type="System.Int32">2023</Property>
7 <Property Name="ISBN" Type="System.String">123-4567890123</P
8 <Property Name="Publisher" Type="System.String">Tech Books P
9 </Object>
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 22/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
10 </Objects>
Tips and Best Practices for Working with
XML in PowerShell
As you delve deeper into the world of XML handling in PowerShell,
consider the following tips and best practices to enhance your workflow
and ensure the reliability of your scripts:
1. Validate XML Structure: Always validate the structure of your XML
files before attempting to manipulate them. This can help you identify
and address any syntax or schema-related issues.
2. Use Error Handling: Incorporate robust error-handling mechanisms in
your scripts to gracefully handle any exceptions or unexpected
scenarios that may arise when working with XML data.
3. Leverage XML Namespaces: If your XML data contains namespaces,
be sure to use the appropriate namespace-aware techniques, such as
the XmlNamespaceManager class, to navigate and extract the relevant
information.
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 23/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
4. Maintain Readability: Use descriptive variable names, well-structured
code, and appropriate formatting to ensure your XML-related
PowerShell scripts remain readable and maintainable over time.
5. Keep your XML structures simple and avoid unnecessary complexity.
By following these tips and best practices, you’ll be well on your way to
becoming a PowerShell XML master, capable of tackling even the most
complex XML-related tasks with ease and efficiency.
Conclusion
To wrap things up, XML manipulation in PowerShell is a powerful tool that
simplifies data management tasks. In this article, We’ve walked through
real-world examples and practical use cases, demonstrating how XML
manipulation in PowerShell can be applied to solve common
requirements.
Whether you’re working with configuration files, data exchange formats,
or any other XML-based tasks, PowerShell has the tools and capabilities
to help you easily conquer those challenges. So, dive into PowerShell and
see how XML can make your data tasks more manageable and effective.
Happy scripting!
💡 Frequently Asked Questions (FAQs)
How do I read an XML file in PowerShell?
How can I query XML using XPath in PowerShell?
How do I create a new XML document in PowerShell?
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 24/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
Can I modify XML content using PowerShell?
How do I save changes to an XML file in PowerShell?
Is it possible to convert XML to other formats using PowerShell?
Can PowerShell work efficiently with large XML files?
How do I extract specific elements or attributes from XML in
PowerShell?
How do I add a new element to an existing XML document?
Can I export a PowerShell object to an XML file?
PowerShell Test-Connection: Ping to Check Network Connectivity
Mastering PowerShell Enum: A Comprehensive Guide
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 25/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
Salaudeen Rajack
I’m an Information Technology expert with two decades of hands-on
experience, 15x certified, specializing in SharePoint, PowerShell,
Microsoft 365, and related products. Over the years, I’ve worked in
various roles including IT Manager, SharePoint Architect,
Administrator, Developer, and Consultant - helping organizations
implement and fine-tune their SharePoint environments. With a
strong technical background and a passion for knowledge sharing, I
love sharing practical tips and lessons learned, through my real-
world articles! Read More
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment *
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 26/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
Name
Email
Notify me of new posts by email.
Post Comment
About Me
Salaudeen Rajack is a globally recognized IT Expert – Specialized in SharePoint, PowerShell,
Office 365 and related Microsoft technologies.
Recent Popular Tags
How to Block User Access in SharePoint Online?
PDF Password Protection in SharePoint Online & OneDrive
Microsoft Graph: How to Find the Drive ID of a Document Library?
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 27/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
Intelligent Version History in SharePoint: All You Need to Know
Microsoft Entra PowerShell Module: A Guide for Admins
Disclaimer
This is my personal blog. Articles written on this blog are from my experience for my own
reference and to help others.
Do not reproduce my content anywhere, in any form without my permission. If any article
written on this blog violates copyright, please contact me! If you have a more elegant solution
on any of the topics discussed – please post a comment, I’ll be happy to hear!
Frequently Asked Questions
How to Run PowerShell Scripts for SharePoint Online?
SharePoint Online: Add Site Collection Administrator using PowerShell
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 28/29
30/10/25, 13:04 XML Manipulation in PowerShell: A Comprehensive Guide - SharePoint Diary
Subscribe to Updates
Enter your email address:
Email Address Subscribe
Blog Awards
Copyright © 2025 SharePoint Diary. All rights reserved.
Support this site Privacy Policy Contact Us
https://www.sharepointdiary.com/2020/11/xml-manipulation-in-powershell-comprehensive-guide.html 29/29