I'm trying to create a PowerShell script that transpose a CSV file from column to rows.
I found examples of doing the opposite (converting row based CSV to column) but I found nothing on column to rows. My problem being that I don't know exactly how many column I'll have. I tried adapting the row to column to column to rows but unsuccessfully.
$a = Import-Csv "input.csv"
$a | FT -AutoSize
$b = @()
foreach ($Property in $a.Property | Select -Unique) {
$Props = [ordered]@{ Property = $Property }
foreach ($Server in $a.Server | Select -Unique){
$Value = ($a.where({ $_.Server -eq $Server -and
$_.Property -eq $Property })).Value
$Props += @{ $Server = $Value }
}
$b += New-Object -TypeName PSObject -Property $Props
}
$b | FT -AutoSize
$b | Out-GridView
$b | Export-Csv "output.csv" -NoTypeInformation
For example my CSV can look like this:
"ID","DATA1" "12345","11111" "54321","11111" "23456","44444"
or this (number of column can vary):
"ID","DATA1","DATA2","DATA3" "12345","11111","22222","33333" "54321","11111",, "23456","44444","55555",
and I would like the script to convert it like this:
"ID","DATA" "12345","11111" "12345","22222" "12345","33333" "54321","11111" "23456","44444" "23456","55555"