How to Export Table to CSV in PowerShell?

Recently, one of my email subscribers emailed to learn about exporting a table to CSV (Comma-Separated Values) using PowerShell. This is a very common requirement in PowerShell. In this tutorial, I will explain how to export table to CSV in PowerShell with different examples.

To export a table to CSV in PowerShell, you can use the Export-Csv cmdlet. First, create an array of custom objects representing your data, then pipe this array to Export-Csv and specify the file path. For example:

$employees = @(
    [PSCustomObject]@{Name="John Doe"; Title="Manager"; Department="Sales"},
    [PSCustomObject]@{Name="Jane Smith"; Title="Developer"; Department="IT"},
    [PSCustomObject]@{Name="Sam Johnson"; Title="Analyst"; Department="Finance"}
)
$employees | Export-Csv -Path "C:\Data\employees.csv" -NoTypeInformation

This command will export the employee data to a CSV file located at C:\Data\employees.csv.

Export Table to CSV in PowerShell

CSV files are widely used because they are easy to read and write and can be opened in various applications like Microsoft Excel, Google Sheets, and many database systems. Exporting data to CSV allows for easy data sharing and analysis.

Basic Syntax of Export-CSV

The primary cmdlet for exporting data to CSV in PowerShell is Export-Csv. Here’s the basic syntax:

Export-Csv -InputObject <Object> -Path <String> [-Delimiter <Char>] [-NoTypeInformation] [-Encoding <Encoding>]

Parameters:

  • -InputObject: Specifies the object to be exported.
  • -Path: Specifies the path to the CSV file.
  • -Delimiter: Specifies a delimiter other than a comma (optional).
  • -NoTypeInformation: Omits the type information from the CSV file (recommended).
  • -Encoding: Specifies the encoding for the CSV file (optional).

Check out How to Export an Array to CSV in PowerShell?

Export Table to CSV in PowerShell Examples

Now, let me show you some examples of exporting a table to a CSV file in PowerShell.

Example 1: Export a Simple Table to CSV

Let’s start with a straightforward example. Suppose we have a table of employees and want to export this data to a CSV file using PowerShell. Then, you can use the PowerShell script below.

$employees = @(
    [PSCustomObject]@{Name="John Doe"; Title="Manager"; Department="Sales"},
    [PSCustomObject]@{Name="Jane Smith"; Title="Developer"; Department="IT"},
    [PSCustomObject]@{Name="Sam Johnson"; Title="Analyst"; Department="Finance"}
)

$employees | Export-Csv -Path "C:\MyFolder\employees.csv" -NoTypeInformation

In this example, we create an array of custom objects representing employees. We then pipe this array to Export-Csv and specify the path to the CSV file. The -NoTypeInformation parameter is used to omit the type information from the CSV file.

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

powershell export table to csv

While CSV files traditionally use commas to separate values, you can specify a different delimiter if needed. For example, to use a semicolon as the delimiter:

$employees = @(
    [PSCustomObject]@{Name="John Doe"; Title="Manager"; Department="Sales"},
    [PSCustomObject]@{Name="Jane Smith"; Title="Developer"; Department="IT"},
    [PSCustomObject]@{Name="Sam Johnson"; Title="Analyst"; Department="Finance"}
)
$employees | Export-Csv -Path "C:\MyFolder\employees_semicolon.csv" -Delimiter ';' -NoTypeInformation

Check out Format an Array as an HTML Table in PowerShell

Example 2: Export Data from a Command

You can also export data directly from a PowerShell command. For example, let’s export a list of running processes to a CSV file using the below PowerShell script.

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

Here, we use the Get-Process cmdlet to get a list of running processes. We then use Select-Object to select specific properties (Name, Id, CPU) and pipe the result to Export-Csv.

Example 3: Export Nested Objects

Exporting nested objects to a CSV file in PowerShell can be a bit tricky. Here’s an example of how to handle nested objects:

$departments = @(
    [PSCustomObject]@{Name="Sales"; Employees=@([PSCustomObject]@{Name="John Doe"; Title="Manager"})},
    [PSCustomObject]@{Name="IT"; Employees=@([PSCustomObject]@{Name="Jane Smith"; Title="Developer"})}
)

$departments | ForEach-Object {
    $_.Employees | Export-Csv -Path "C:\MyFolder\$($_.Name)_employees.csv" -NoTypeInformation
}

In this example, we have a nested structure where each department has a list of employees. We use ForEach-Object to iterate over each department and export its employees to a separate CSV file.

Here is the exact output in the screenshot below:

Export Table to CSV in PowerShell

Conclusion

In this tutorial, I explained how to export a table to CSV in PowerShell using the Export-CSV cmdlet with some real examples. If you still have any questions, feel free to leave a comment below.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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