PowerShell Logo Small

Export-Csv



This is the built-in help made by Microsoft for the command 'Export-Csv', in PowerShell version 5 - as retrieved from Windows version 'Microsoft Windows Server 2012 R2 Standard' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Converts objects into a series of comma-separated (CSV) strings and saves the strings in a CSV file.

SYNTAX


Export-Csv [[-Path] [<String>]] [[-Delimiter] [<Char>]] [-Append] [-Encoding {Unicode | UTF7 | UTF8 | ASCII | UTF32 | BigEndianUnicode | Default | OEM}] [-Force]
[-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable [<System.String>]] [-LiteralPath [<String>]] [-NoClobber]
[-NoTypeInformation] -InputObject <PSObject> [-Confirm] [-WhatIf] [<CommonParameters>]
Export-Csv [[-Path] [<String>]] [-Append] [-Encoding {Unicode | UTF7 | UTF8 | ASCII | UTF32 | BigEndianUnicode | Default | OEM}] [-Force] [-InformationAction
{SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable [<System.String>]] [-LiteralPath [<String>]] [-NoClobber] [-NoTypeInformation]
[-UseCulture] -InputObject <PSObject> [-Confirm] [-WhatIf] [<CommonParameters>]



Search powershellhelp.space

DESCRIPTION


The Export-CSV cmdlet creates a CSV file of the objects that you submit. Each object is represented as a line or row of the CSV. The row consists of a comma-separated list
of the values of object properties. You can use this cmdlet to create spreadsheets and share data with programs that take CSV files as input.


NOTE: Do not format objects before sending them to the Export-CSV cmdlet. If you do, the format properties are represented in the CSV file, instead of the properties of the
original objects. To export only selected properties of an object, use the Select-Object cmdlet.



<

RELATED LINKS

Online Version: http://go.microsoft.com/fwlink/p/?linkid=293957
ConvertFrom-Csv
ConvertTo-Csv
Format-Table
Import-Csv
Select-Object

REMARKS

<

Examples


-------------------------- EXAMPLE 1 --------------------------

PS C:\>get-process wmiprvse | select-object basePriority,ID,SessionID,WorkingSet | export-csv -path data.csv



This command selects a few properties of the WmiPrvse process and exports them to a CSV file named Data.csv.










-------------------------- EXAMPLE 2 --------------------------

PS C:\>get-process | export-csv processes.csv
PS C:\>get-process | export-csv processes.csv

# In processes.csv
#TYPE System.Diagnostics.Process
__NounName,Name,Handles,VM,WS,PM,NPM,Path,Company,CPU,FileVersion,... Process,powershell,626,201666560,76058624,61943808,11960,C:\WINDOWS...
Process,powershell,257,151920640,38322176,37052416,7836,C:\WINDOWS\...



This command exports objects representing the processes on the computer to the Processes.csv file in the current directory. Because it does not specify a delimiter, a comma
(,) is used to separate the fields in the file.










-------------------------- EXAMPLE 3 --------------------------

PS C:\>get-process | export-csv processes.csv -Delimiter ";"

# In processes.csv
#TYPE System.Diagnostics.Process
__NounName;Name;Handles;VM;WS;PM;NPM;Path;Company;CPU;FileVersion;... Process;powershell;626;201666560;76058624;61943808;11960;C:\WINDOWS...
Process;powershell;257;151920640;38322176;37052416;7836;C:\WINDOWS\...



This command exports objects representing the processes on the computer to the Processes.csv file in the current directory. It uses the Delimiter parameter to specify the
semicolon (;). As a result, the fields in the file are separated by semicolons.










-------------------------- EXAMPLE 4 --------------------------

PS C:\>get-process | export-csv processes.csv -UseCulture



This command exports objects representing the processes on the computer to the Processes.csv file in the current directory. It uses the UseCulture parameter to direct
Export-CSV to use the delimiter specified by the ListSeparator property of the current culture.










-------------------------- EXAMPLE 5 --------------------------

PS C:\>get-process | export-csv processes.csv -NoTypeInformation
PS C:\>get-process | export-csv processes.csv -NoTypeInformation

# In processes.csv
__NounName,Name,Handles,VM,WS,PM,NPM,Path,Company,CPU,FileVersion,... Process,powershell,626,201666560,76058624,61943808,11960,C:\WINDOWS...
Process,powershell,257,151920640,38322176,37052416,7836,C:\WINDOWS\...



This command exports objects representing the processes on the computer to the Processes.csv file in the current directory. It uses the NoTypeInformation parameter to
suppress the type information in the file.










-------------------------- EXAMPLE 6 --------------------------

The first command uses the Get-ChildItem cmdlet to do a recursive search in the D: drive for files with the .ps1 file name extension. It uses a pipeline operator to sends
the results to the Where-Object cmdlet, which gets only files that were created after January 1, 2011, and then saves them in the ScriptFiles variable.
PS C:\>$scriptFiles = Get-ChildItem D:\* -include *.ps1 –recurse | where-object {$_.creationtime –gt "01/01/2011"}

The second command uses the Select-Object cmdlet to select the relevant properties of the script files. It saves the revised results in the ScriptFiles variable.
PS C:\>$scriptFiles = $scriptFiles | select-object –property Name, CreationTime, LastWriteTime, IsReadOnly

The third command uses a pipeline operator (|) to send the script file information in the ScriptFiles variable to the Export-CSV cmdlet. The command uses the Path parameter
to specify the output file and the Append parameter to add the new script data to the end of the output file, instead of replacing the existing file contents.
PS C:\>$scriptFiles | export-csv –append –path \\Archive01\Scripts\Scripts.csv



These commands add information about new Windows PowerShell scripts to a script inventory file.






-------------------------- EXAMPLE 7 --------------------------

The first command shows how to select properties of an object and export them to a CSV file. This command uses the Get-Date cmdlet to get the current date and time. It uses
the Select-Object cmdlet to select the desired properties, and the Export-CSV cmdlet to export the object and its properties to the Date.csv file. The output shows the
expected content in the Date.csv file.
PS C:\>get-date | select-object –property DateTime, Day, DayOfWeek, DayOfYear | export-csv –path Date.csv

#In Date.csv:"DateTime","Day","DayOfWeek","DayOfYear""Tuesday, October 05, 2010 2:45:13 PM","5","Tuesday","278"

The second command shows that when you use the Format-Table cmdlet to format your data before exporting it, the output is not useful.
PS C:\>get-date | format-table –property DateTime, Day, DayOfWeek, DayOfYear | export-csv –path Date.csv

#In Date.csv: "ClassId2e4f51ef21dd47e99d3c952918aff9cd","pageHeaderEntry","pageFooterEntry","autosizeInfo","shapeInfo","groupingEntry""033ecb2bc07a4d43b5ef94ed5a35d280",,,,"M
icrosoft.PowerShell.Commands.Internal.Format.TableHeaderInfo","9e210fe47d09416682b841769c78b8a3",,,,,"27c87ef9bbda4f709f6b4002fa4af63c",,,,,"4ec4f0187cb04f4cb6973460dfe252df"
,,,,,"cf522b78d86c486691226b40aa69e95c",,,,,



This example demonstrates one of most common problems that users encounter when using the Export-CSV cmdlet. It explains how to recognize and avoid this error.

Because a CSV file has a table format, it might seem natural to use the Format-Table cmdlet to format the data in a table to prepare it for export as a CSV file. Also, the
Format-Table cmdlet allows you to select object properties easily.

However, when you format the data in a table and then export it, you are exporting a table object, not your original data object. The resulting CSV file is not useful.

This example shows how to select object properties by using the Select-Object cmdlet, and it shows the Export-CSV formats the data correctly in a CSV file without any
preparatory formatting by another cmdlet.