powershell导出csv添加标题?

时间:2014-10-14 09:02:03

标签: powershell

 $employees = @()

#Add newly created object to the array
#$employees += $employee
for ($i=1;$i -le 2;$i++)
{
    $employee = New-Object System.Object
    $employee | Add-Member -MemberType NoteProperty -Name "Creation Time" -Value $(Get-date)
    $employee | Add-Member -MemberType NoteProperty -Name "Employee ID" -Value $($i.ToString("D2"))
    $employees += $employee
    Start-Sleep -Seconds 1
}


#Finally, use Export-Csv to export the data to a csv file
$employees | Export-Csv -NoTypeInformation -Path "c:\temp\test\EmployeeList.csv"

结果是

Creation Time     Employee ID

2014/10/14 1530      1

2014/10/14 1530      2

2014/10/14 1532      1

2014/10/14 1532      2

如何添加标题...例如

report<--title

Creation Time       Employee ID

2014/10/14 1530      1

2014/10/14 1530      2

2014/10/14 1532      1

2014/10/14 1532      2

1 个答案:

答案 0 :(得分:1)

使用Export-Csv cmdlet是不可能的。您必须将文件读入临时变量,写入标题行并添加csv内容:

$csvPath = "c:\temp\test\EmployeeList.csv"
$eList = gc $csvPath
echo "report<--title" > $csvPath
$eList >> $csvPath