< Back

ConvertFrom-Csv

Tue Jan 29, 2019 10:21 pm

NAME ConvertFrom-Csv



SYNOPSIS

Converts object properties in comma-separated value (CSV) format into CSV versions of the original objects.





SYNTAX

ConvertFrom-Csv [-InputObject] <PSObject[]> [[-Delimiter] <Char>] [-Header <String[]>] [<CommonParameters>]



ConvertFrom-Csv [-InputObject] <PSObject[]> [-Header <String[]>] -UseCulture [<CommonParameters>]





DESCRIPTION

The ConvertFrom-Csv cmdlet creates objects from CSV variable-length strings that are generated by the ConvertTo-Csv cmdlet.



You can use the parameters of this cmdlet to specify the column header row, which determines the property names of the resulting objects, to

specify the item delimiter, or to direct this cmdlet to use the list separator for the current culture as the delimiter.



The objects that ConvertFrom-CSV creates are CSV versions of the original objects. The property values of the CSV objects are string versions of

the property values of the original objects. The CSV versions of the objects do not have any methods.



You can also use the Export-Csv and Import-Csv cmdlets to convert objects to CSV strings in a file (and back). These cmdlets are the same as the

ConvertTo-CSV and ConvertFrom-CSV cmdlets, except that they save the CSV strings in a file.





PARAMETERS

-Delimiter <Char>

Specifies the delimiter that separates the property values in the CSV strings. The default is a comma (,). Enter a character, such as a colon

(:). To specify a semicolon (;), enclose it in quotation marks.



If you specify a character other than the delimiter used in the CSV strings, ConvertFrom-CSV cannot create objects from the CSV strings.

Instead, it returns the strings.



Required? false

Position? 1

Default value None

Accept pipeline input? False

Accept wildcard characters? false



-Header <String[]>

Specifies an alternate column header row for the imported string. The column header determines the names of the properties of the object that

ConvertFrom-CSV creates.



Enter a comma-separated list of the column headers. Enclose each item in quotation marks (single or double). Do not enclose the header string

in quotation marks. If you enter fewer column headers than there are columns, the remaining columns will have no headers. If you enter more

headers than there are columns, the extra headers are ignored.



When using the Header parameter, omit the column header string from the CSV strings. Otherwise, this cmdlet creates an extra object from the

items in the header row.



Required? false

Position? named

Default value None

Accept pipeline input? False

Accept wildcard characters? false



-InputObject <PSObject[]>

Specifies the CSV strings to be converted to objects. Enter a variable that contains the CSV strings or type a command or expression that gets

the CSV strings. You can also pipe the CSV strings to ConvertFrom-CSV .



Required? true

Position? 0

Default value None

Accept pipeline input? True (ByPropertyName, ByValue)

Accept wildcard characters? false



-UseCulture [<SwitchParameter>]

Indicates that this cmdlet uses the list separator for the current culture as the string delimiter. The default is a comma (,).



To find the list separator for a culture, use the following command: `(Get-Culture).TextInfo.ListSeparator`. If you specify a character other

than the delimiter used in the CSV strings, ConvertFrom-CSV cannot create objects from the CSV strings. Instead, it returns the strings.



Required? true

Position? named

Default value False

Accept pipeline input? False

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).



INPUTS

System.String

You can pipe CSV strings to this cmdlet.





OUTPUTS

System.Management.Automation.PSObject

This cmdlet returns the objects described by the properties in the CSV strings.





NOTES





* Because the imported objects are CSV versions of the object type, they are not recognized and formatted by the Windows PowerShell type

formatting entries that format the non-CSV versions of the object type.



In CSV format, each object is represented by a comma-separated list of the property values of the object. The property values are converted to

strings (by using the ToString() method of the object), so they are generally represented by the name of the property value. This cmdlet does

not export the methods of the object.



*



Example 1: Convert processes on the local computer to CSV format



PS C:\\>$p = get-process | convertto-csv

PS C:\\> $p | convertfrom-csv



These commands convert the processes on the local computer into CSV format and then restore them to object form.



The first command uses the Get-Process cmdlet to get the processes on the local computer. A pipeline operator (|) sends them to the ConvertTo-CSV

cmdlet, which converts the process object to CSV format. The CSV strings are saved in the $p variable.



The second command uses a pipeline operator to send the CSV strings in the $p variable to the ConvertFrom-CSV cmdlet. The cmdlet converts the CSV

strings into CSV versions of the original process objects.

Example 2: Convert a data object to CSV format and then to CSV object format



PS C:\\>$Date = Get-Date | ConvertTo-Csv -Delimiter ";"

PS C:\\> ConvertFrom-Csv -InputObject $Date -Delimiter ";"



These commands convert a data object to CSV format and then to CSV object format.



The first command uses the Get-Date cmdlet to get the current date and time. A pipeline object (|) sends the date to the ConvertTo-Csv cmdlets,

which converts the date object to a series of CSV strings. The command uses the Delimiter parameter to specify a semicolon delimiter. The strings

are saved in the $date variable.



The second command uses the ConvertFrom-CSV cmdlet to convert the CSV strings in the $date variable back to object format. The command uses the

InputObject parameter to specify the CSV strings and the Delimiter parameter to specify the semicolon delimiter.

Example 3: Use the header parameter to change the names of properties



PS C:\\>$J = Start-Job -ScriptBlock {Get-Process } | ConvertTo-Csv

PS C:\\> $Header = "MoreData","StatusMessage","Location","Command","State","Finished","InstanceId","SessionId","Name","ChildJobs","Output","Error","

Progress","Verbose","Debug","Warning","StateChanged"

# Delete header from $J

PS C:\\> $J = $J[0], $J[2..($J.count - 1)]

PS C:\\> $J | ConvertFrom-Csv -Header $Header

MoreData : True

StatusMessage :

Location : localhost

Command : get-process

State : Running

Finished : System.Threading.ManualResetEvent

InstanceId : 6fcb6578-7f42-4d93-9f23-9937f6aac1a2

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 :



This example shows how to use the Header parameter of ConvertFrom-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 ConvertTo-CSV cmdlet, which converts the job object to CSV format. An assignment operator (=) saves the

resulting CSV in the $J variable.



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

instead of JobStateInfo.



The third command deletes the original header (the second line) from the CSV strings and returns it to the $J variable.



The fourth command uses the ConvertFrom-CSV cmdlet to convert the CSV strings to a CSV version of the job object. The command uses a pipeline

operator to send the content in $J to ConvertFrom-CSV . The resulting object has MoreData and State properties, as specified by the header.

Example 4: Convert CSV strings of service objects



PS C:\\>(Get-Culture).textinfo.listseparator

PS C:\\> ConvertFrom-Csv -InputObject $Services -UseCulture



The command uses the ConvertFrom-CSV cmdlet to convert CSV strings of service objects that had been converted by the ConvertTo-CSV cmdlet. The

command uses the UseCulture parameter to direct ConvertFrom-CSV to use the delimiter (list separator) of the current culture.



When using the UseCulture parameter, be sure that the list separator of the current culture matches the delimiter used in the CSV strings.

Otherwise, ConvertFrom-CSV cannot generate objects from the CSV strings.



In this example, a Get-Culture command was used to verify the list separator, before the ConvertFrom-CSV command was used.



RELATED LINKS

Online Version: http://go.microsoft.com/fwlink/?LinkId=821752

ConvertTo-Csv

Export-Csv

Import-Csv