1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| $InputCSV = 'C:\temp\Group.csv'
$OutputCSV = 'C:\temp\GroupTXT'
$A = import-csv $InputCSV | Group-Object sAMAccountName-Group
# get the maximum number of ITS-Number values for a single sAMAccountName-Group
$max = 0
$A | foreach {if ($_.Count -gt $max) {$max = $_.Count}}
$header = ""
1..$max | ForEach { $header += ('ITS-' + $_ + ',') } # create the header for the CSV
$header += "sAMAccountName-Group" # add the last column header
Set-Content -Path $OutputCSV -Value $header
$A | ForEach {
$empty = $max - $_.Count # how many empty columns in this row?
$its = @()
ForEach ($G in $_.Group) {$its += $G.'ITS-Number'} # get each ITS-Number for this sAMAccountName-Group
$str = $its -join ','
$str += (',' * $empty) + ',' + $_.Name # add the appropriate number of empty columns and the sAMAccountName-Group
$str | Write-Output
} | Add-Content -Path $OutputCSV |
Partager