Powershell transpose rows into columns

◇◆丶佛笑我妖孽 提交于 2019-12-02 12:58:22

Start by finding all relevant machine names, we'll need those later:

$Rows = Import-Csv d:\report3.csv
$MachineNames = $Rows |Select-Object -ExpandProperty MachineName |Sort -Unique

Next up, group all entries by Endtime, and add the values from all rows that are grouped together into a single object:

$ConsolidatedRows = $Rows |Group-Object EndTime |ForEach-Object {
    $NewRowProperties = @{ EndTime = [DateTime]::Parse($_.Name) }
    foreach($Row in $_.Group)
    {
        $NewRowProperties.Add($Row.MachineName,$Row.TotalDataSizeBytes)
    }
    New-Object psobject -Property $NewRowProperties
}

And then finally, select the EndTime property and all the MachineName-specific properties using the names we grabbed earlier:

$ConsolidatedRows |Select-Object @("EndTime";$MachineNames) |Export-Csv .\finalReport.csv -NoTypeInformation
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!