Export Arrays to CSV in PowerShell

Over the years, I’ve found that exporting data to CSV files is one of the most common tasks when working with PowerShell. Recently, one of my clients asked us to export arrays to CSV in PowerShell. There are various methods to do so.

In this tutorial, I will explain several ways to export arrays to CSV in PowerShell, using examples from my decade-plus of experience.

Let’s dive in and explore these methods step by step!

Method 1 – Using Export-Csv (The Standard Approach)

The Export-Csv cmdlet is PowerShell’s built-in solution for converting objects to CSV format. This is the method I use most frequently in my day-to-day work.

Here’s a basic example where I’m creating an array of US state data and exporting it to CSV:

# Create an array of custom objects
$statesArray = @(
    [PSCustomObject]@{State="California"; Capital="Sacramento"; Population=39538223}
    [PSCustomObject]@{State="Texas"; Capital="Austin"; Population=29145505}
    [PSCustomObject]@{State="Florida"; Capital="Tallahassee"; Population=21538187}
    [PSCustomObject]@{State="New York"; Capital="Albany"; Population=20201249}
)

# Export the array to CSV
$statesArray | Export-Csv -Path "C:\Projects\USStates.csv" -NoTypeInformation

The -NoTypeInformation parameter omits the type information line at the top of the CSV file, which is usually what you want.

Here is an exact output in the screenshot below:

Export Arrays to CSV in PowerShell

If you need to open this file with Excel and are concerned about character encoding (especially for international data), you might want to add the -Encoding parameter:

$statesArray | Export-Csv -Path "C:\Projects\USStates.csv" -NoTypeInformation -Encoding UTF8

Check out How to Add Items to an Array in PowerShell

Method 2 – Append to an Existing CSV File

Sometimes you need to add data to an existing CSV file rather than creating a new one. Here’s how I handle that:

# Create additional state data
$moreStates = @(
    [PSCustomObject]@{State="Illinois"; Capital="Springfield"; Population=12812508}
    [PSCustomObject]@{State="Pennsylvania"; Capital="Harrisburg"; Population=13002700}
)

# Append to the existing CSV file
$moreStates | Export-Csv -Path "C:\Projects\USStates.csv" -NoTypeInformation -Append

The -Append parameter is key here – it adds the new data to the end of the existing file instead of overwriting it.

You can see the exact output in the screenshot below;

powershell export array to csv

Check out PowerShell Array of Strings

Method 3 – Converting Hashtables to CSV

When working with hashtables instead of custom objects, you need to convert them first. Here’s my approach:

# Create an array of hashtables
$hashArray = @(
    @{State="Ohio"; Capital="Columbus"; Population=11799448},
    @{State="Georgia"; Capital="Atlanta"; Population=10711908}
)

# Convert hashtables to PSObjects and export
$hashArray | ForEach-Object { [PSCustomObject]$_ } | Export-Csv -Path "C:\Projects\MoreStates.csv" -NoTypeInformation

The ForEach-Object { [PSCustomObject]$_ } part converts each hashtable into a proper PSCustomObject that works well with the Export-Csv cmdlet.

Check out PowerShell Write to File UTF-8

Method 4 – Control CSV Field Order

One issue I frequently encounter is controlling the order of fields in the CSV output. Here’s how I manage that:

# Create an array with properties in random order
$dataArray = @(
    [PSCustomObject]@{Population=5893718; State="Wisconsin"; Capital="Madison"},
    [PSCustomObject]@{State="Minnesota"; Population=5706494; Capital="St. Paul"}
)

# Export with specific property order
$dataArray | Select-Object State, Capital, Population | Export-Csv -Path "C:\Projects\OrderedStates.csv" -NoTypeInformation

The Select-Object cmdlet lets you specify exactly which properties you want and in what order.

You can see the exact output in the screenshot below:

How to Export Arrays to CSV in PowerShell

Check out How to Write to File Line by Line in PowerShell

Method 5 – Using ConvertTo-Csv for More Control

Sometimes you might want to manipulate the CSV data before writing it to a file. The ConvertTo-Csv cmdlet converts objects to CSV format without writing to a file:

# Convert to CSV format
$csvData = $statesArray | ConvertTo-Csv -NoTypeInformation

# Modify the header row
$csvData[0] = '"State Name","State Capital","State Population"'

# Write to file
$csvData | Out-File -FilePath "C:\Projects\CustomHeaderStates.csv" -Encoding utf8

This approach gives you control over every aspect of the CSV output, including customizing header names.

Check out How to Get File Size Using PowerShell?

Method 6 – Export Multi-Dimensional Arrays

When dealing with multi-dimensional arrays in PowerShell, I flatten them before exporting:

# Create a multi-dimensional array
$multiArray = @(
    @("Michigan", "Lansing", 10077331),
    @("New Jersey", "Trenton", 9288994)
)

# Convert to objects and export
$objects = $multiArray | ForEach-Object {
    [PSCustomObject]@{
        State = $_[0]
        Capital = $_[1]
        Population = $_[2]
    }
}

$objects | Export-Csv -Path "C:\Projects\FlattenedStates.csv" -NoTypeInformation

This approach transforms each sub-array into a proper object with named properties.

Method 7 – Handle Large Data Sets with Streaming

For very large arrays that might cause memory issues, I use a streaming approach:

# Function to generate sample data
function Get-StateData {
    # This could be a database query or other large data source
    for ($i = 0; $i -lt 1000; $i++) {
        [PSCustomObject]@{
            ID = $i
            State = "State$i"
            Capital = "Capital$i"
            Population = (Get-Random -Minimum 100000 -Maximum 40000000)
        }
    }
}

# Stream directly to CSV without storing the entire array in memory
Get-StateData | Export-Csv -Path "C:\Projects\LargeStateData.csv" -NoTypeInformation

This pattern processes one object at a time through the pipeline, avoiding the need to store the entire dataset in memory.

Check out Get the Last Modified Date of a File in PowerShell

Best Practices for CSV Export in PowerShell

Through my years of PowerShell experience, I’ve developed these best practices:

  1. Always use -NoTypeInformation unless you specifically need the type information.
  2. Consider character encoding issues – UTF8 is usually a safe choice.
  3. Use Select-Object to control field order and to remove unwanted properties.
  4. Test your CSV files with the applications that will consume them (Excel, database tools, etc.).
  5. For very large datasets, use streaming techniques rather than building large arrays in memory.

In this tutorial, I have explained how to export an array to csv in PowerShell using various methods with examples.

I hope you found this guide helpful! If you have any questions or suggestions for other PowerShell topics you’d like to see covered, please let me know in the comments below.

You may also like:

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App