ConvertTo-Csv

Converts objects into a series of comma-separated value (CSV) strings.

语法

Delimiter (默认值)

ConvertTo-Csv
    [-InputObject] <psobject>
    [[-Delimiter] <char>]
    [-NoTypeInformation]
    [<CommonParameters>]

UseCulture

ConvertTo-Csv
    [-InputObject] <psobject>
    [-UseCulture]
    [-NoTypeInformation]
    [<CommonParameters>]

说明

The ConvertTo-CSV cmdlet returns a series of comma-separated value (CSV) strings that represent the objects that you submit. You can then use the ConvertFrom-Csv cmdlet to recreate objects from the CSV strings. The objects converted from CSV are string values of the original objects that contain property values and no methods.

You can use the Export-Csv cmdlet to convert objects to CSV strings. Export-CSV is similar to ConvertTo-CSV, except that it saves the CSV strings to a file.

The ConvertTo-CSV cmdlet has parameters to specify a delimiter other than a comma or use the current culture as the delimiter.

示例

Example 1: Convert an object to CSV

This example converts a Process object to a CSV string.

Get-Process -Name 'PowerShell' | ConvertTo-Csv -NoTypeInformation
"Name","SI","Handles","VM","WS","PM","NPM","Path","Company","CPU","FileVersion", ...
"powershell","11","691","2204036739072","175943680","132665344","33312", ...

The Get-Process cmdlet gets the Process object and uses the Name parameter to specify the PowerShell process. The process object is sent down the pipeline to the ConvertTo-CSV cmdlet. The ConvertTo-CSV cmdlet converts the object to CSV strings. The NoTypeInformation parameter removes the #TYPE information header from the CSV output.

Example 2: Convert a DateTime object to CSV

This example converts a DateTime object to a CSV string.

$Date = Get-Date
ConvertTo-Csv -InputObject $Date -Delimiter ';' -NoTypeInformation
"DisplayHint";"DateTime";"Date";"Day";"DayOfWeek";"DayOfYear";"Hour";"Kind";"Millisecond";"Minute";"Month";"Second";"Ticks";"TimeOfDay";"Year"
"DateTime";"Friday, January 4, 2019 14:40:51";"1/4/2019 00:00:00";"4";"Friday";"4";"14";"Local";"711";"40";"1";"51";"636822096517114991";"14:40:51.7114991";"2019"

The Get-Date cmdlet gets the DateTime object and saves it in the $Date variable. The ConvertTo-Csv cmdlet converts the DateTime object to strings. The InputObject parameter uses the DateTime object stored in the $Date variable. The Delimiter parameter specifies a semicolon to separate the string values. The NoTypeInformation parameter removes the #TYPE information header from the CSV output.

Example 3: Convert the PowerShell event log to CSV

This example converts the Windows event log for PowerShell to a series of CSV strings.

(Get-Culture).TextInfo.ListSeparator
Get-WinEvent -LogName 'Windows PowerShell' | ConvertTo-Csv -UseCulture -NoTypeInformation
,
"Message","Id","Version","Qualifiers","Level","Task","Opcode","Keywords","RecordId", ...
"Error Message = System error","403",,"0","4","4",,"36028797018963968","46891","PowerShell", ...

The Get-Culture cmdlet uses the nested properties TextInfo and ListSeparator and displays the current culture's default list separator. The Get-WinEvent cmdlet gets the event log objects and uses the LogName parameter to specify the log file name. The event log objects are sent down the pipeline to the ConvertTo-Csv cmdlet. The ConvertTo-Csv cmdlet converts the event log objects to a series of CSV strings. The UseCulture parameter uses the current culture's default list separator as the delimiter. The NoTypeInformation parameter removes the #TYPE information header from the CSV output.

参数

-Delimiter

Specifies the delimiter to separate the property values in CSV strings. The default is a comma (,). Enter a character, such as a colon (:). To specify a semicolon (;) enclose it in single quotation marks.

参数属性

类型:Char
默认值:comma (,)
支持通配符:False
不显示:False

参数集

Delimiter
Position:1
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-InputObject

Specifies the objects that are converted to 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 ConvertTo-CSV.

参数属性

类型:PSObject
默认值:None
支持通配符:False
不显示:False

参数集

(All)
Position:0
必需:True
来自管道的值:True
来自管道的值(按属性名称):True
来自剩余参数的值:False

-NoTypeInformation

Removes the #TYPE information header from the output. This parameter became the default in PowerShell 6.0 and is included for backwards compatibility.

参数属性

类型:SwitchParameter
默认值:None
支持通配符:False
不显示:False
别名:NTI

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-UseCulture

Uses 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.

参数属性

类型:SwitchParameter
默认值:None
支持通配符:False
不显示:False

参数集

UseCulture
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

输入

PSObject

You can pipe any object that has an Extended Type System (ETS) adapter to ConvertTo-CSV.

输出

String

The CSV output is returned as a collection of strings.

备注

In CSV format, each object is represented by a comma-separated list of its property value. The property values are converted to strings using the object's ToString() method. The strings are represented by the property value name. ConvertTo-CSV does not export the object's methods.

The CSV strings are output as follows:

  • By default the first string contains the #TYPE information header followed by the object type's fully qualified name. For example, #TYPE System.Diagnostics.Process.
  • If NoTypeInformation is used the first string includes the column headers. The headers contain the first object's property names as a comma-separated list.
  • The remaining strings contain comma-separated lists of each object's property values.

When you submit multiple objects to ConvertTo-CSV, ConvertTo-CSV orders the strings based on the properties of the first object that you submit. If the remaining objects do not have one of the specified properties, the property value of that object is Null, as represented by two consecutive commas. If the remaining objects have additional properties, those property values are ignored.