CSV is a popular file format in order to store tabular data. CSV is actually a simple text file where every column or part of the data is delimited with a comma in general. The CSV name comes from a command-separated value. Another useful feature of the CSV format is the CSV files can be easily converted into Excel files and can be opened with the Excel tools. PowerShell provides the Export-Csv command in order to create or export data into the CSV file.
Export-CSV Command Example
We will start with a simple CSV export example. As the Export-CSV command export specified data into a CSV file we will use the process list as the data. We will use the Get-Process to list currently running processes and related information in a tabular or structured way. In the following example, we will export running processes and related information into the CSV file named Process.csv.
Get-Process | Export-CSV Process.csv
The content of the exported file named Process.csv is like below. We can see that the first line of the CSV file contains the header information
Alternatively the CSV file can be specified by using the -Path option like below.
Get-Process | Export-CSV -Path .\Process.csv
We can export the CSV file into different path then the current working directory. The -Path option can be used different path or absolute path with the CSV file name. Also the -Path option can be used to export in to different drive or partition.
Get-Process | Export-CSV -Path D:\Data\Process.csv
Remove Type Information
When used with another PowerShell command the Export-CSV command exports the type information of the output in the first line of the CSV file by default. This type of information can be removed with the -NoTypeInformation option like below.
Get-Process | Export-CSV -NoTypeInformation -Path Process.csv
Set Semicolon As Delimiter Instead of Comma
The name of the CSV file comes from the comma and the comma is used as default delimiter. The default delimiter command can be changed into another sign like semicolon. The semicolon is also very popular for the CSV file delimiter. The -Delimiter option can be used to set semicolon as separator.
Get-Process | Export-CSV -Semicolon ';' -Path Process.csv
Also the -Delimiter option can be also used set different delimiters like point, characters etc.
Overwrite Read-Only CSV File
In some cases, the Export-CSV command can be used to overwrite an existing CSV file. Even this CSV file can be set as read-only where overwriting is not possible in a normal way. Generally the following error or exception occurs when we try to write into the existing read-only file.
Export-Csv : Access to the path 'C:\Process.csv' is denied. At line:1 char:15 + Get-Process | Export-Csv -Path .\ReadOnly.csv -NoTypeInformation + ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OpenError: (:) [Export-Csv], UnauthorizedAccessException + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand
We can use the -Force option in order to overwrite existing files.
Get-Process | Export-CSV -Force -Path Process.csv
Append Existing CSV File
Another useful feature of the Export-CSV command is the ability to append an existing CSV file. This is very useful if we need to add new data to the existing data. This will not overwrite existing data only add new lines. The -Append option can be used to append an existing CSV file.
Get-Process | Export-CSV -Append Process.csv
If you get an error like “The appended object does not have a property that corresponds to the following column: Version” you can try the -Append option with the -Force option like below.
Get-Process | Export-CSV -Append -Force Process.csv