Export-Csv

Export-Csv – A Handy PowerShell Tool for Creating CSV Files

Welcome back to Wahmans PowerShell blog! Today we’ll be diving into a super useful cmdlet called Export-Csv. According to Microsoft, this cmdlet “Converts objects into a series of character-separated value (CSV) strings and saves the strings to a file.” In simpler terms, it allows you to take any objects in PowerShell and quickly save them in a CSV file format, which is perfect for sharing, reporting, or importing into Excel or other tools.

Why Use Export-Csv?

If you’re working with structured data in PowerShell (like lists of objects), you’ll soon find that exporting that data in a convenient format like CSV is incredibly handy. Whether you’re creating reports, saving logs, or preparing data for other applications, Export-Csv has got you covered.

Getting Started: 4 Examples of Export-Csv in Action

Example 1 – Exporting Simple Object Data (Beginner)

This is a classic beginner scenario. Let’s say we have some user data in a list of PowerShell objects. We want to export it:

$users = @(
    [PSCustomObject]@{Name='Alice'; Age=30; Country='Sweden'},
    [PSCustomObject]@{Name='Bob'; Age=25; Country='USA'}
)

$users | Export-Csv -Path 'C:\temp\users.csv' -NoTypeInformation

We use -NoTypeInformation to avoid including the type header in the CSV.

Example 2 – Get-Process Output to CSV (Intermediate)

Another practical use case is exporting currently running processes:

Get-Process | Select-Object Name, Id, CPU | Export-Csv -Path 'C:\temp\processes.csv' -NoTypeInformation

This is great for quick diagnostics, automation, or auditing.

Example 3 – Filtering and Exporting Active Directory Users (Advanced)

If you are using PowerShell modules like ActiveDirectory, you might want to export user details to review:

Get-ADUser -Filter * -Property Name, Department, EmailAddress | 
Select-Object Name, Department, EmailAddress | 
Export-Csv -Path 'C:\temp\ad_users.csv' -NoTypeInformation

This example assumes the Active Directory module is installed and you are running PowerShell with appropriate permissions.

Example 4 – Exporting Nested Objects with Custom Formatting (Advanced)

Export-Csv doesn’t handle nested objects well by default, so here we flatten the structure first:

$computers = @(
    [PSCustomObject]@{Name='Server01'; IP='192.168.1.10'; Specs=[PSCustomObject]@{CPU='Intel Xeon'; RAM='32GB'}},
    [PSCustomObject]@{Name='Server02'; IP='192.168.1.11'; Specs=[PSCustomObject]@{CPU='AMD EPYC'; RAM='64GB'}}
)

$flattened = $computers | ForEach-Object {
    [PSCustomObject]@{
        Name = $_.Name
        IP = $_.IP
        CPU = $_.Specs.CPU
        RAM = $_.Specs.RAM
    }
}

$flattened | Export-Csv -Path 'C:\temp\computers.csv' -NoTypeInformation

By flattening the nested Specs object, we make sure the CSV is clean and readable.

Wrapping Up

Whether you’re just starting with PowerShell or doing advanced scripting, Export-Csv is a powerful cmdlet that helps bridge the gap between your scripts and the rest of the world via universally readable CSV files.

Happy scripting, and I will see you in the next post!

Leave a Reply

Your email address will not be published. Required fields are marked *