Have you ever received an XML file from a vendor, API, or internal system and struggled to make sense of it in Excel or a database? XML is great for storing structured data, but it’s not easy to analyze directly. So, you can use PowerShell to convert an XML file to a CSV file.
In this tutorial, I’ll show you exactly how I handle XML-to-CSV conversions in real-world IT environments. We’ll cover multiple methods, from quick one-liners to flexible scripts, so whether you’re just starting with PowerShell or you’re an experienced sysadmin, you’ll walk away with practical solutions you can use immediately.
For the examples, I am using the XML file below:
<?xml version="1.0" encoding="UTF-8"?>
<Employees>
<Employee>
<ID>1001</ID>
<Name>Sarah Johnson</Name>
<Department>IT</Department>
<Position>Senior Developer</Position>
<Email>sarah.johnson@company.com</Email>
<Salary>85000</Salary>
<HireDate>2019-03-15</HireDate>
<Location>New York</Location>
</Employee>
<Employee>
<ID>1002</ID>
<Name>Michael Chen</Name>
<Department>Marketing</Department>
<Position>Marketing Manager</Position>
<Email>michael.chen@company.com</Email>
<Salary>72000</Salary>
<HireDate>2020-07-22</HireDate>
<Location>California</Location>
</Employee>
<Employee>
<ID>1003</ID>
<Name>Emily Rodriguez</Name>
<Department>HR</Department>
<Position>HR Specialist</Position>
<Email>emily.rodriguez@company.com</Email>
<Salary>58000</Salary>
<HireDate>2021-01-10</HireDate>
<Location>Texas</Location>
</Employee>
<Employee>
<ID>1004</ID>
<Name>David Thompson</Name>
<Department>Finance</Department>
<Position>Financial Analyst</Position>
<Email>david.thompson@company.com</Email>
<Salary>65000</Salary>
<HireDate>2018-11-05</HireDate>
<Location>Illinois</Location>
</Employee>
<Employee>
<ID>1005</ID>
<Name>Jennifer Lee</Name>
<Department>IT</Department>
<Position>System Administrator</Position>
<Email>jennifer.lee@company.com</Email>
<Salary>78000</Salary>
<HireDate>2020-02-18</HireDate>
<Location>Washington</Location>
</Employee>
</Employees>Check out PowerShell Convert XML to Object
Method 1: Using [xml] Type Accelerator with Export-Csv
The most straightforward way to handle XML in PowerShell is by loading the file into an XML object using [xml]. Once loaded, we can navigate the nodes and export them.
# Load XML file
[xml]$xmlData = Get-Content -Path "C:\Data\employees.xml"
# Select the nodes you want
$records = $xmlData.Employees.Employee
# Export to CSV
$records | Export-Csv -Path "C:\Data\employees.csv" -NoTypeInformationHere, I’m assuming the XML has a structure like <Employees><Employee>...</Employee></Employees>. The Export-Csv cmdlet handles the conversion into a clean, tabular format.
I have used the above PowerShell script on my Mac OS. The only change is the file path.
# Load XML file
[xml]$xmlData = Get-Content -Path "/Users/bijay/Documents/Files/employees.xml"
# Select the nodes you want
$records = $xmlData.Employees.Employee
# Export to CSV
$records | Export-Csv -Path "/Users/bijay/Documents/Files/employees.csv" -NoTypeInformationI executed the above script, and you can see the exact output in the screenshot below:

This method works best when the XML structure is consistent and not deeply nested. It’s my go-to for vendor data feeds like employee lists, product catalogs, or system logs.
Method 2: Using Select-Xml for Flexible Queries
When XML files are complex or nested, I prefer using Select-Xml. It allows XPath queries, which means I can precisely extract the nodes I need.
# Use XPath to select specific nodes
$nodes = Select-Xml -Path "C:\Data\orders.xml" -XPath "//Order"
# Extract attributes and elements
$nodes.Node | Select-Object OrderID, CustomerID, OrderDate |
Export-Csv -Path "C:\Data\orders.csv" -NoTypeInformationThis approach is powerful because XPath gives me fine-grained control. For example, if I only want completed orders or specific attributes, I can filter directly in the query.
For large enterprise XML files (think ERP or CRM exports), this method saves hours of manual cleanup.
Method 3: Using ConvertTo-Csv for Quick Conversions
Sometimes I don’t want to write directly to a file but just need to preview the CSV format in the console. That’s where ConvertTo-Csv shines.
[xml]$xmlData = Get-Content "C:\Data\inventory.xml"
$xmlData.Inventory.Item |
Select-Object ID, Name, Quantity |
ConvertTo-Csv -NoTypeInformationThis outputs the CSV-formatted text directly in the PowerShell console. I often use this for quick testing before finalizing an Export-Csv command.
Convert Simple XML to CSV Using PowerShell Cmdlets
For straightforward XML files, PowerShell’s built-in features make conversion easy. Let’s say you have employee data from a U.S. company in XML format.
How it works:
We’ll load the XML file, select the nodes we need, and export them to CSV. This approach is perfect for flat XML structures.
# Load XML data
[xml]$xml = Get-Content -Path 'C:\Projects\us-employees.xml'
# Select relevant nodes and export to CSV
$xml.Employees.Employee |
Select-Object Name, State, Department |
Export-Csv -Path 'C:\Projects\us-employees.csv' -NoTypeInformationHere is the exact output in the screenshot below:

PowerShell automatically parses the XML and lets you access each employee as an object. Selecting the properties you want and exporting them to CSV is both fast and reliable. This method is ideal for most business data scenarios.
Check out How to Use PowerShell Export-CSV cmdlet?
Handle Nested or Complex XML Structures
U.S. enterprise XML data often contains nested elements. In these cases, we need to flatten the structure before converting.
How it works:
We’ll use PowerShell to loop through nested nodes, extract the required data, and build custom objects for CSV export.
[xml]$xml = Get-Content -Path 'C:\Projects\us-orders.xml'
$orders = foreach ($order in $xml.Orders.Order) {
[PSCustomObject]@{
OrderID = $order.OrderID
Customer = $order.Customer.Name
State = $order.Customer.State
Product = $order.Product.Name
Quantity = $order.Product.Quantity
Date = $order.Date
}
}
$orders | Export-Csv -Path 'C:\Projects\us-orders.csv' -NoTypeInformationBy creating custom objects, we can flatten any nested structure and ensure our CSV contains only the columns we need. This is crucial for reporting and analytics in large U.S. organizations.
You can see the exact output in the screenshot below:

Read Create Folder Structure from CSV using PowerShell
Convert XML from Web Services or APIs
Many U.S. companies pull XML from web APIs. PowerShell can fetch this data and convert it on the fly.
We’ll use Invoke-WebRequest to get XML data from a URL, then process it as before.
# Fetch XML from a web API
$response = Invoke-WebRequest -Uri 'https://api.uscompany.com/employees.xml'
[xml]$xml = $response.Content
# Convert to CSV
$xml.Employees.Employee |
Select-Object Name, State, Department |
Export-Csv -Path 'C:\Projects\api-employees.csv' -NoTypeInformationFetching and converting data in one script saves time and keeps your reporting pipeline up-to-date with real-time U.S. data.
Check out Remove Blank Lines from CSV Files Using PowerShell
Best Practices for XML to CSV Conversion
Here are a few lessons I’ve learned over the years:
- Validate XML first – Use
Test-Xmlor just load it with[xml]to catch malformed files early. - Always specify
-NoTypeInformation– Otherwise, PowerShell adds extra header rows in your CSV. - Use
Select-Object– This ensures only the fields you care about are exported. - Document your XPath queries – Future you (or your team) will thank you.
In this tutorial, I explained how to convert an XML file to a .CSV file using different methods. We looked at three main methods: [xml] with Export-Csv for simple cases, Select-Xml for complex queries, and ConvertTo-Csv for quick previews.
You may also like the following tutorials:
- PowerShell Import-Csv Cmdlet
- Get the First and Last Line of a CSV File in PowerShell
- Convert CSV to HTML Table in PowerShell
- Export Table to CSV in PowerShell
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.