PowerShell Logo Small

Import-Csv



This is the built-in help made by Microsoft for the command 'Import-Csv', in PowerShell version 2 - as retrieved from Windows version 'Microsoft® Windows Vista™ Ultimate ' 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 object properties in a comma-separated value (CSV) file into CSV versions of the original objects.

SYNTAX


Import-CSV [[-Delimiter] <char>] [-Path] <string[]> [-Header <string[]>] [<CommonParameters>]
Import-CSV -UseCulture [-Path] <string[]> [-Header <string[]>] [<CommonParameters>]



Search powershellhelp.space

DESCRIPTION


The Import-CSV cmdlet creates objects from CSV variable-length files that are generated by the Export-CSV cmdlet.

You can use the parameters of the Import-CSV cmdlet to specify the column header row, which determines the property names of the resulting object
s; to specify the item delimiter; or to direct Import-CSV to use the list separator for the current culture as the item delimiter.

The objects that Import-CSV creates are CSV versions of the original objects. The property values of the CSV objects are string versions of the p
roperty values of the original objects. The CSV versions of the objects do not have any methods.

You can also use the ConvertTo-CSV and ConvertFrom-CSV cmdlets to convert objects to CSV strings (and back). These cmdlets are the same as the Ex
port-CSV and Import-CSV cmdlets, except that they do not save the CSV strings in a file.



<

RELATED LINKS

Online version: http://go.microsoft.com/fwlink/?LinkID=113341
Export-CSV
ConvertTo-CSV
ConvertFrom-CSV

REMARKS

<

Examples


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

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

C:\PS> $p = import-CSV processes.csv

C:\PS> $p | get-member

TypeName: CSV:System.Diagnostics.Process

Name MemberType Definition
---- ---------- ----------
Equals Method System.Boolean Equals(Object obj)
GetHashCode Method System.Int32 GetHashCode()
GetType Method System.Type GetType()
ToString Method System.String ToString()
BasePriority NoteProperty System.String BasePriority=8
Company NoteProperty System.String Company=Microsoft Corporation
...

C:\PS> $p | out-gridview



Description
-----------
This example shows how to export and then import a CSV file of Microsoft .NET Framework objects.

The first command uses the Get-Process cmdlet to get the process on the local computer. It uses a pipeline operator (|) to send the process objec
ts to the Export-CSV cmdlet, which exports the process objects to the Processes.csv file in the current directory.

The second command uses the Import-CSV cmdlet to import the processes in the Import-CSV file. Then it saves the resulting process objects in the
$p variable.

The third command uses a pipeline operator to pipe the imported objects to the Get-Member cmdlets. The result shows that they are CSV:System.Diag
nostic.Process objects, not the System.Diagnostic.Process objects that Get-Process returns.

Also, because there is no entry type in the formatting files for the CSV version of the process objects, these objects are not formatted in the s
ame way that standard process objects are formatted.

To display the objects, use the formatting cmdlets, such as Format-Table and Format-List, or pipe the objects to Out-GridView.








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

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

C:\PS> $p = import-csv processes.csv -Delimiter :



Description
-----------
This example shows how to use the Delimiter parameter of Import-CSV. In this example, the processes are exported to a file that uses a colon (:)
as a delimiter.

When importing, the Import-CSV file uses the Delimiter parameter to indicate the delimiter that is used in the file.








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

C:\PS>$p = import-csv processes.csv -UseCulture

C:\PS> (get-culture).textinfo.listseparator

,



Description
-----------
This example shows how to use the UseCulture parameter of Import-CSV.

The first command imports the objects in the Processes.csv file into the $p variable. It uses the UseCulture parameter to direct Import-CSV to us
e the list separator defined for the current culture.

The second command displays the list separator for the current culture. It uses the Get-Culture cmdlet to get the current culture. It uses the do
t (.) method to get the TextInfo property of the current culture and the ListSeparator property of the object in TextInfo. In this example, the c
ommand returns a comma.








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

C:\PS>start-job -scriptblock { get-process } | export-csv jobs.csv

C:\PS> $header = "MoreData","StatusMessage","Location","Command","State","Finished","InstanceId","SessionId","Name","ChildJobs","Output","Error",
"Progress","Verbose","Debug","Warning","StateChanged"

# Delete header from file
C:\PS> $a = (get-content jobs.csv)
C:\PS> $a = $a[0], $a[2..($a.count - 1)]
C:\PS> $a > jobs.csv

C:\PS> $j = import-csv jobs.csv -header $header

C:\PS> $j

MoreData : True
StatusMessage :
Location : localhost
Command : get-process
State : Running
Finished : System.Threading.ManualResetEvent
InstanceId : 135bdd25-40d6-4a20-bd68-05282a59abd6
SessionId : 1
Name : Job1
ChildJobs : System.Collections.Generic.List`1[System.Management.Automation.Job]
Output : System.Management.Automation.PSDataCollection`1[System.Management.Automation.PSObject]
Error : System.Management.Automation.PSDataCollection`1[System.Management.Automation.ErrorRecord]
Progress : System.Management.Automation.PSDataCollection`1[System.Management.Automation.ProgressRecord]
Verbose : System.Management.Automation.PSDataCollection`1[System.String]
Debug : System.Management.Automation.PSDataCollection`1[System.String]
Warning : System.Management.Automation.PSDataCollection`1[System.String]
StateChanged :



Description
-----------
This example shows how to use the Header parameter of Import-CSV to change the names of properties in the resulting imported object.

The first command uses the Start-Job cmdlet to start a background job that runs a Get-Process command on the local computer. A pipeline operator
(|) sends the resulting job object to the Export-CSV cmdlet, which converts the job object to CSV format. An assignment operator (=) saves the re
sulting CSV in the Jobs.csv file.

The second command saves a header in the $header variable. Unlike the default header, this header uses "MoreData" instead of "HasMoreData" and "S
tate" instead of "JobStateInfo".

The next three commands delete the original header (the second line) from the Jobs.csv file.

The sixth command uses the Import-CSV cmdlet to import the Jobs.csv file and convert the CSV strings into a CSV version of the job object. The co
mmand uses the Header parameter to submit the alternate header. The results are stored in the $j variable.

The seventh command displays the object in the $j variable. The resulting object has "MoreData" and "State" properties, as shown in the command o
utput.








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

C:\PS>".\processes.csv" | import-csv



Description
-----------
This command imports the objects from the Processes.csv file.