CmdID / PowerShell / Cmdlets / Export
Export-Csv
Converts objects into a series of character-separated value (CSV) strings and saves the strings to a file. PowerShell reference
From the PowerShell reference
12 parameters
Alias epcsv
What it does
Converts objects into a series of character-separated value (CSV) strings and saves the strings to a file. The Export-Csv cmdlet creates a CSV file of the objects that you submit. Each object is a row that includes a character-separated list of the object's property values. You can use the Export-Csv cmdlet to create spreadsheets and share data with programs that accept CSV files as input. Do not format objects before sending them to the Export-Csv cmdlet. If Export-Csv receives formatted objects the CSV file contains the format properties rather than the object properties. To export only selected properties of an object, use the Select-Object cmdlet.
Export-Csv [[-Path] <string>] [[-Delimiter] <char>] -InputObject <psobject> [-LiteralPath <string>] [-Force] [-NoClobber] [-Encoding <string>] [-Append] [-NoTypeInformation] [-WhatIf] [-Confirm] [<CommonParameters>]Export-Csv [[-Path] <string>] -InputObject <psobject> [-LiteralPath <string>] [-Force] [-NoClobber] [-Encoding <string>] [-Append] [-UseCulture] [-NoTypeInformation] [-WhatIf] [-Confirm] [<CommonParameters>]
Export process properties to a CSV file
Get-Process -Name WmiPrvSE |
Select-Object -Property BasePriority,Id,SessionId,WorkingSet |
Export-Csv -Path .\WmiData.csv -NoTypeInformation
Import-Csv -Path .\WmiData.csvIdentifyExport processes to a comma-delimited file
Get-Process | Export-Csv -Path .\Processes.csv -NoTypeInformation
Get-Content -Path .\Processes.csvIdentifyExport processes to a semicolon delimited file
Get-Process | Export-Csv -Path .\Processes.csv -Delimiter ';' -NoTypeInformation
Get-Content -Path .\Processes.csvIdentifyExport processes using the current culture's list separator
(Get-Culture).TextInfo.ListSeparator
Get-Process | Export-Csv -Path .\Processes.csv -UseCulture -NoTypeInformation
Get-Content -Path .\Processes.csvIdentifyExport processes with type information
Get-Process | Export-Csv -Path .\Processes.csv
Get-Content -Path .\Processes.csvIdentifyExport and append objects to a CSV file
$AppService = Get-Service -DisplayName Application |
Select-Object -Property DisplayName, Status
$AppService | Export-Csv -Path .\Services.Csv -NoTypeInformation
Get-Content -Path .\Services.Csv
$WinService = Get-Service -DisplayName Windows |
Select-Object -Property DisplayName, Status
$WinService | Export-Csv -Path .\Services.csv -NoTypeInformation -Append
Get-Content -Path .\Services.CsvIdentifyFormat cmdlet within a pipeline creates unexpected results
Get-Date | Select-Object -Property DateTime, Day, DayOfWeek, DayOfYear |
Export-Csv -Path .\DateTime.csv -NoTypeInformation
Get-Content -Path .\DateTime.csvIdentifyUsing the Force parameter to overwrite read-only files
New-Item -Path .\ReadOnly.csv -ItemType File
Set-ItemProperty -Path .\ReadOnly.csv -Name IsReadOnly -Value $true
Get-Process | Export-Csv -Path .\ReadOnly.csv -NoTypeInformationIdentifyUsing the Force parameter with Append
$Content = [pscustomobject]@{Name = 'PowerShell'; Version = '7.0'}
$Content | Export-Csv -Path .\ParmFile.csv -NoTypeInformation
$AdditionalContent = [pscustomobject]@{Name = 'Windows PowerShell'; Edition = 'Desktop'}
$AdditionalContent | Export-Csv -Path .\ParmFile.csv -NoTypeInformation -AppendIdentifyIts page in the reference: Export-Csv.
Parameters
-AppendUse this parameter so that Export-Csv adds CSV output to the end of the specified file. Without this parameter, Export-Csv replaces the file contents without warning.
This parameter was introduced in Windows PowerShell 3.0.-DelimiterSpecifies a delimiter to separate the property values. The default is a comma (,).
Enter a character, such as a colon (:). To specify a semicolon (;), enclose it in single quotation marks. To specify escaped special characters such as tab ( t ), enclose it in double quotation marks.-EncodingSpecifies the encoding for the exported CSV file. The default value is ASCII.
The acceptable values for this parameter are as follows:
- ASCII Uses ASCII (7-bit) character set.
- BigEndianUnicode Uses UTF-16 with the big-endian byte order.
- Default Uses the encoding that corresponds to the system's active code page (usually ANSI).
- OEM Uses the encoding that corresponds to the system's current OEM code page.
- Unicode Uses UTF-16 with the little-endian byte order.
- UTF7 Uses UTF-7.
- UTF8 Uses UTF-8.
- UTF32 Uses UTF-32 with the little-endian byte order.-ForceThis parameter allows Export-Csv to overwrite files with the Read Only attribute.
When Force and Append parameters are combined, objects that contain mismatched properties can be written to a CSV file. Only the properties that match are written to the file. The mismatched properties are discarded.-InputObjectSpecifies the objects to export as CSV strings. Enter a variable that contains the objects or type a command or expression that gets the objects. You can also pipe objects to Export-Csv.-LiteralPathSpecifies the path to the CSV output file. Unlike Path, the value of the LiteralPath parameter is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, use single quotation marks. Single quotation marks tell PowerShell not to interpret any characters as escape sequences.-NoClobberUse this parameter so that Export-Csv does not overwrite an existing file. By default, if the file exists in the specified path, Export-Csv overwrites the file without warning.-NoTypeInformationRemoves the #TYPE information header from the output. This parameter became the default in PowerShell 6.0 and is included for backwards compatibility.-PathA required parameter that specifies the location to save the CSV output file.-UseCultureUses the list separator for the current culture as the item delimiter. To find the list separator for a culture, use the following command: (Get-Culture).TextInfo.ListSeparator.-ConfirmPrompts you for confirmation before running the cmdlet.-WhatIfPrevents the cmdlet from being processed or making changes. The output shows what would happen if the cmdlet were run.