Working with XML data is a common task for system administrators, developers, and IT professionals. Whether you are dealing with configuration files, API responses, or exported reports, XML is still widely used for structured data storage and transfer.
In PowerShell, XML can be directly converted into objects, which makes working with data much easier. Instead of manually parsing strings, you can navigate XML like an object hierarchy, filter nodes, and even export to other formats.
In this tutorial, we’ll cover different ways to convert XML to objects in PowerShell, how to query data effectively, and explore real-world use cases.
What is XML and Why Use It in PowerShell?
XML (Extensible Markup Language) is a plain-text format designed for storing and transferring structured information. It is widely used in applications, configuration files, API responses, system logs, and many business workflows.
Some common places you’ll encounter XML include:
- Application configuration files.
- Web service responses (SOAP, legacy APIs).
- Windows Server and IIS logs.
- Data export from enterprise systems.
When you load XML into PowerShell as an object, you can leverage all PowerShell’s object manipulation features such as filtering, looping, and exporting to CSV or JSON. This transforms static XML into actionable data.
Check out Convert XML to CSV in PowerShell
Understanding PowerShell Objects
PowerShell is an object-oriented scripting language. Everything you work with—files, processes, services, or XML—is an object.
An object contains:
- Properties: data fields (like
Name,ID,Path). - Methods: actions you can perform on the object.
Converting XML to objects means you can:
- Use dot notation to traverse nodes.
- Filter with
Where-Object. - Pipe results into other cmdlets (
Export-Csv,ConvertTo-Json, etc.).
Check out Create XML Files with Content Using PowerShell
Methods to Convert XML into PowerShell Objects
There are multiple ways to work with XML in PowerShell. Let’s go through each method with examples.
1. Using the [xml] Type Accelerator
The [xml] type accelerator is the most common way to convert XML into an object.
[xml]$xmlData = Get-Content -Path "C:\data\file.xml"This casts the data into an XML object model, allowing you to use dot notation for navigation.
Example XML:
$xmlData = [xml]@"
<Employees>
<Employee>
<ID>101</ID>
<Name>John Doe</Name>
<Department>HR</Department>
</Employee>
<Employee>
<ID>102</ID>
<Name>Jane Smith</Name>
<Department>IT</Department>
</Employee>
</Employees>
"@To access the first employee’s name:
$xmlData.Employees.Employee[0].NameThis outputs:
John DoeHere is the exact output in the screenshot below:

Read How to Test If a File Exists in PowerShell?
2. Using Get-Content with [xml]
When you use Get-Content without [xml], it simply reads XML as text. By wrapping it in [xml], you get a structured document.
$raw = Get-Content "C:\data\file.xml"
$xmlObject = [xml]$raw
$xmlObject.Employees.Employee | ForEach-Object { $_.Name }This will list out:
John Doe
Jane Smith3. Using Select-Xml
The Select-Xml cmdlet is powerful when you want to use XPath queries to target specific nodes.
Select-Xml -Path "C:\data\file.xml" -XPath "//Employee/Name"This returns nodes matching <Name>. You can extract text via:
(Select-Xml -Path "C:\data\file.xml" -XPath "//Employee/Name").Node.InnerTextWhich outputs:
John Doe
Jane SmithCheck out PowerShell Select-String Plus Next Lines
4. Parsing XML from a String
Sometimes XML comes from inline strings or dynamically generated content. PowerShell can parse this directly.
$xmlString = "<Root><Item>Hello</Item></Root>"
$xmlObject = [xml]$xmlString
$xmlObject.Root.ItemOutput:
Hello5. Converting XML from Web or API Calls
Many APIs still return XML. You can retrieve and parse it easily:
$response = Invoke-WebRequest -Uri "https://example.com/data.xml"
[xml]$xmlObject = $response.Content
$xmlObject.Root.ElementThis lets you traverse API results as objects instead of manually parsing text.
Check out PowerShell Curl [Guide to Web Requests in PowerShell]
Navigating XML Objects in PowerShell
Once XML is converted into an object, you can use different approaches to access and filter data.
Accessing Nodes with Dot Notation
$xmlData.Employees.Employee[0].DepartmentIterating Over Multiple Nodes
foreach ($emp in $xmlData.Employees.Employee) {
Write-Output "$($emp.ID) - $($emp.Name)"
}Output:
101 - John Doe
102 - Jane SmithHandling Nested XML
When XML gets complex with multiple levels, you can chain properties.
$xmlData = [xml]@"
<Projects>
<Project>
<Name>Migration</Name>
<Tasks>
<Task>
<ID>1</ID>
<Description>Plan migration</Description>
</Task>
<Task>
<ID>2</ID>
<Description>Execute migration</Description>
</Task>
</Tasks>
</Project>
</Projects>
"@Access tasks:
$xmlData.Projects.Project.Tasks.Task | ForEach-Object { $_.Description }Here is the exact output in the screenshot below:

Using XPath Queries
XPath is extremely useful for XML queries. With Select-Xml, you can filter nodes:
Select-Xml -Xml $xmlData -XPath "//Employee[Department='IT']/Name"Output:
Jane SmithWorking with Large XML Files
Large XML files can consume memory. To optimize:
- Use
Select-Xmlinstead of loading the entire object. - Filter data early to reduce looping.
- Process line by line for massive XML files.
For instance, to target nodes without loading the whole file:
Select-Xml -Path "C:\data\large.xml" -XPath "//Log[Level='Error']"Error Handling in XML Conversion
XML can fail to load if malformed. Always use error handling:
try {
[xml]$xmlData = Get-Content "C:\data\broken.xml"
} catch {
Write-Host "Error loading XML: $($_.Exception.Message)"
}This prevents script crashes and lets you handle issues gracefully.
Conclusion
XML remains an important format in many systems, and PowerShell provides simple ways to work with it. By converting XML into objects, you gain the ability to query, filter, and export data with ease.
We explored multiple conversion methods—using [xml], Select-Xml, string parsing, and API parsing—along with navigation, export options, large file handling, and real examples.
You may also like:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.