How to Convert XML to CSV in PowerShell

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" -NoTypeInformation

Here, 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" -NoTypeInformation

I executed the above script, and you can see the exact output in the screenshot below:

PowerShell Convert XML to CSV

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" -NoTypeInformation

This 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 -NoTypeInformation

This 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' -NoTypeInformation

Here is the exact output in the screenshot below:

PowerShell Convert XML to CSV

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' -NoTypeInformation

By 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:

PowerShell Convert XML to CSV Examples

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' -NoTypeInformation

Fetching 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-Xml or 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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.