How to Use PowerShell Export-CSV cmdlet?

In this tutorial, I will explain how to use the Export-Csv cmdlet in PowerShell to export data into CSV files. As a developer, I once faced the challenge of exporting user information from an Active Directory to a CSV file for a client in New York. Here, I will explain in detail.

PowerShell Export-Csv Cmdlet

The Export-Csv cmdlet in PowerShell allows you to export data objects to a CSV file. This is particularly useful for system administrators and developers who need to generate reports or transfer data between systems.

Here is a real example that I, as an administrator, face.

Imagine you are a system administrator in Los Angeles, tasked with generating a monthly report of all users in the Active Directory, including their names, email addresses, and office locations. Instead of manually copying this data, you can automate the process using PowerShell and the Export-Csv cmdlet.

Syntax of Export-Csv

The basic syntax of the Export-Csv cmdlet is:

Get-Command | Export-Csv -Path "C:\MyNewFolder\file.csv" -NoTypeInformation
  • Get-Command: This is a placeholder for any PowerShell command that generates output.
  • -Path: Specifies the path where the CSV file will be saved.
  • -NoTypeInformation: Omits the type information from the CSV file, making it cleaner.

Check out How to Use PowerShell Import-Csv Cmdlet?

PowerShell Export-CSV Examples

Now, let me show you a few examples of the PowerShell Export-CSV cmdlet.

Example 1: Export Active Directory Users

Let’s start with a practical example of exporting Active Directory users to a CSV file. Suppose you need to export the first name, last name, and email address of all users in the New York office.

Then, you can write the below PowerShell script.

Import-Module ActiveDirectory
Get-ADUser -Filter * -Property GivenName,Surname,EmailAddress,Office | 
    Select-Object GivenName,Surname,EmailAddress,Office | 
    Export-Csv -Path "C:\Reports\NY_ADUsers.csv" -NoTypeInformation

In this example:

  • Import-Module ActiveDirectory: Imports the Active Directory module.
  • Get-ADUser -Filter * -Property GivenName,Surname,EmailAddress,Office: Retrieves all users with specified properties.
  • Select-Object: Selects the properties to include in the CSV.
  • Export-Csv: Exports the selected data to a CSV file.

Check out How to Export Table to CSV in PowerShell?

Example 2: Export System Processes

Suppose you need to export the list of running processes on a server in Chicago to analyze performance issues. Then, you can use the PowerShell script below.

Get-Process | 
    Select-Object ProcessName,CPU,Id,StartTime | 
    Export-Csv -Path "C:\Reports\Chicago_Processes.csv" -NoTypeInformation

In this example:

  • Get-Process: Retrieves the list of running processes.
  • Select-Object: Selects the relevant properties.
  • Export-Csv: Exports the data to a CSV file.

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

export-csv PowerShell

Read How to Export an Array to CSV in PowerShell?

Example 3: Export Event Logs

Here is another example that explains how to export event logs using the Export-CSV PowerShell cmdlet.

Here is an example. Suppose you need to export event logs from a server in San Francisco for a comprehensive audit.

Get-EventLog -LogName Application -Newest 100 | 
    Select-Object EventID,Source,EntryType,Message,TimeGenerated | 
    Export-Csv -Path "C:\Reports\SF_EventLogs.csv" -NoTypeInformation

In this example:

  • Get-EventLog -LogName Application -Newest 100: Retrieves the 100 most recent application event logs.
  • Select-Object: Selects the relevant properties.
  • Export-Csv: Exports the data to a CSV file.

Let me show you another example of using the Export-CSV PowerShell cmdlet.

Read PowerShell Test-Path

Example 4: Using Export-Csv with Arrays

You can also export data from arrays. For instance, if you have an array of custom objects representing sales data in Dallas, you can export it to CSV:

$salesData = @(
    [PSCustomObject]@{Name="John Doe"; Sales=1500; Region="Dallas"},
    [PSCustomObject]@{Name="Jane Smith"; Sales=2000; Region="Dallas"}
)

$salesData | Export-Csv -Path "C:\Reports\Dallas_SalesData.csv" -NoTypeInformation

Here is the exact output in the screenshot below:

How to Use PowerShell Export-CSV cmdlet

Example 5: Append Data to Existing CSV Files

To append data to an existing CSV file, you can use the -Append parameter of the Export-CSV PowerShell cmdlet.

Here is the complete PowerShell script.

Get-Service | 
    Select-Object Name,Status | 
    Export-Csv -Path "C:\Reports\Services.csv" -Append -NoTypeInformation

Conclusion

In this tutorial, I explained how to use the Export-CSV cmdlet in PowerShell with a few examples. I have also explained five different examples related to the PowerShell Export-CSV cmdlet.

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.