How to Export Data to CSV in PowerShell

Migel Hewage Nimesha Feb 02, 2024
  1. Export Data to CSV With the Export-CSV PowerShell Cmdlet
  2. Export Data to CSV With the ConvertTo-CSV PowerShell Cmdlet
How to Export Data to CSV in PowerShell

CSV file is just a delimited text file with a specific format applied to its content. The format is very similar to a simple table structure, as shown in the following example.

For example,

Name, NIC, Phone

John, 394829G,  +671392392

Risabh, 32909340J, +12328833

The CSV is the short form for Comma Separated Values. As the file type(CSV) suggests, it separates data values using commas. There are different tools available to generate CSV files with data.

In this article, we will discuss how to export data to a CSV file in PowerShell.

Export Data to CSV With the Export-CSV PowerShell Cmdlet

The Export-CSV is one of the PowerShell cmdlets that can be used to export data in CSV format. This cmdlet is relatively easy to use, and a few properties are available too.

the Export-CSV Cmdlet Syntax

<data_objects/content> | Export-Csv -Path <path>
  • <data_objects/content>: The data objects/content being feed to the Export-CSV cmdlet.
  • Export-CSV: The cmdlet that is responsible for exporting data in CSV format.
  • -Path: The path where the csv file to be saved.

Example 01

We will use the Get-Process cmdlet to return some data objects, and those can be fed to the Export-CSV cmdlet to export in CSV format.

Get-Process

Output:

Handles	NPM(k)	PM(K)	WS(K)	CPU(s)	Id		SI 		ProcessName
313		14		3712	10756	7.69	39500	0		ApplicationFrameHost
507		30		22864	22836	2.11	18156	2		armsvc

There can be several process objects returned. We can use the pipe (|) operator to feed the returned objects to the Export-CSV cmdlet. Then you can check the exported CSV content using the Get-Content cmdlet.

Get-Process | Export-Csv -Path .\ProcessObjects.csv
Get-Content -Path .\ProcessObjects.csv

Output:

"ProcessName","SI","Handles","VM","WS","PM","NPM", .....
"armsvc","2","511","2203597099008","35364864","21979136","30048", ...
"xyz","0","501","299393939","9982824","0021000","393393", ...

There are few useful properties available to alter the default behavior of the Export-CSV cmdlet.

Example 02

Get-Process | Export-Csv -Path .\ProcessObjects.csv -Delimiter ';'
Get-Content -Path .\ProcessObjects.csv

Output:

"ProcessName";"SI";"Handles";"VM";"WS";"PM";"NPM"; .....
"armsvc";"2";"511";"2203597099008";"35364864";"21979136";"30048"; ...

The -Delimiter option changes the delimiter from the default , to the specified symbol.

Export Data to CSV With the ConvertTo-CSV PowerShell Cmdlet

The ConvertTo-CSV cmdlet is almost similar to the Export-CSV cmdlet discussed above. The only difference between the two is Export-csv cmdlet saves the CSV strings to a file, but it is not the case with ConvertTo-CSV. It just returns the Comma-separated value Strings.

the ConvertTo-CSV Cmdlet Syntax

<data_objects/content> | ConvertTo-Csv
  • <data_objects/content>: The data objects/content being feed to the ConvertTo-CSV cmdlet.
  • ConvertTo-CSV: The cmdlet that is responsible for returning CSV strings.

Example

Get-Process | ConvertTo-Csv

Output:

"ProcessName","SI","Handles","VM","WS","PM","NPM", .....
"armsvc","2","511","2203597099008","35364864","21979136","30048", ...
"xyz","0","501","299393939","9982824","0021000","393393", ...p

Therefore, both Export-CSV and ConvertTo-CSV cmdlets are useful tools for generating CSV formatted data using PowerShell.

Migel Hewage Nimesha avatar Migel Hewage Nimesha avatar

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.

Related Article - PowerShell CSV