The Export-Csv command writes data from objects to a text file, for example:
Get-Process | Export-Csv processes.csv
By default, Export-Csv will write a comma-delimited file using ASCII encoding and will completely overwrite any file using the same name.
Export-Csv may be used to add lines to an existing file using the Append parameter. When the Append parameter is used, the input object must have each of the fields listed in the CSV header or an error will be thrown unless the Force parameter is used:
PS> Get-Process powershell | Select-Object Name, Id | Export-Csv .Processes.csv
Get-Process explorer | Select-Object Name | Export-Csv .Processes.csv
-Append
Export-Csv : Cannot append CSV content to the following file: .Processes.csv.
The appended object does not have a property that corresponds to the following column: Id. To continue with mismatched properties, add the -Force parameter, and then retry the command.
At line:2 char:51
+ ... ershell_ise | Select-Object Name | Export-Csv .Processes.csv -Append
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Id:String) [Export-Csv],
InvalidOperationException
+ FullyQualifiedErrorId : CannotAppendCsvWithMismatchedPropertyNames,Microsoft.PowerShell.Commands.ExportCsvCommand
If the Append parameter is used and the input object has more fields than the CSV, the extra fields will be silently dropped when writing the CSV file. For example, the value held in Id will be ignored when writing the results to the existing CSV file:
Get-Process powershell | Select-Object Name | Export-Csv .Processes.csv Get-Process explorer | Select-Object Name, Id | Export-Csv .Processes.csv -Append
Export-Csv will write a header line to each file which details the .NET type it has just exported. If the preceding example is used, that will be the following:
#TYPE Selected.System.Diagnostics.Process
Export-Csv can be instructed to exclude this header using the NoTypeInformation parameter:
Get-Process | Export-Csv processes.csv -NoTypeInformation
ConvertTo-Csv is similar to Export-Csv, except that instead of writing content to a file, content is written as command output:
PS> Get-Process powershell | Select-Object Name, Id | ConvertTo-Csv
#TYPE Selected.System.Diagnostics.Process
"Name","Id"
"powershell","404"
Both Export-Csv and ConvertTo-Csv are limited in what they can do with arrays of objects. For example, ConvertTo-Csv is unable to display the values that are in an array:
PS> [PSCustomObject]@{
Name = "Numbers"
Value = 1, 2, 3, 4, 5
} | ConvertTo-Csv -NoTypeInformation
"Name","Value"
"Numbers","System.Object[]"
The value it writes is taken from theToString method, which is called on the property called Value, for example:
PS> $object = [PSCustomObject]@{
Name = "Numbers"
Value = 1, 2, 3, 4, 5
}
$object.Value.ToString()
System.Object[]
If a CSV file is expected to hold the content of an array, code must be written to convert it into a suitable format. For example, the content of the array can be written after converting it to a string:
PS> [PSCustomObject]@{
Name = "Numbers"
Value = 1, 2, 3, 4, 5
} | ForEach-Object {
$_.Value = $_.Value -join ', '
$_
} | ConvertTo-Csv -NoTypeInformation
"Name","Value"
"Numbers","1, 2, 3, 4, 5"
In the preceding example, the value of the property is joined using a comma followed by a space. The modified object (held in $_) is passed on to the ConvertTo-Csv command.