Change arraylist rows to column using Powershell

Viewed 518

I am trying to change the format of an arraylist after I have used

group-object

to count all the entries in the list.

this is a sample

$list = [System.Collections.ArrayList]@()

$list = "letter","solo","nap","nap","nap","sharp","ignite","tap","tap","tap","tap","evoke"

$list |  Group-Object | select name,count 

This is the sample output

Name   Count
----   -----
letter     1
solo       1
nap        3
sharp      1
ignite     1
tap        4
evoke      1

What I would like is

 letter   solo   nap     sharp   ignite  tap   evoke
--------  -----  ----    ----  -----   ------  ----   
    1       1      3      4     1        4       1

Then when exporting to excel it would format like this

Excel

Everything I have tried doesn't seem to pay off, or even get close to what I am trying to do and I think I am missing something obvious or have hit my PowerShell skill limitations. Could someone please help. Thank you

4 Answers

Skip Group-Object altogether - instead, use a dictionary to keep track of the count, then cast the whole dictionary to a custom object:

$properties = [ordered]@{}

$list |ForEach-Object {
  $properties[$_]++
}

$counts = [pscustomobject]$properties

$counts will now hold an object like what you describe, formatting as a table gives you:

PS C:\> $counts |Format-Table

letter solo nap sharp ignite tap evoke
------ ---- --- ----- ------ --- -----
     1    1   3     1      1   4     1

You may try something like:

$list = [System.Collections.ArrayList]@()
$list = "letter","solo","nap","nap","nap","sharp","ignite","tap","tap","tap","tap","evoke"
$group = $list |  Group-Object | select name,count 
$a = [PSCustomObject]@{}
foreach ($item in $group) {
    $a | Add-Member -NotePropertyName $item.name -NotePropertyValue $item.count
}
$a | ft

enter image description here

You could create a PSObject, add the properties to it with Add-Member, then format the output to a table with Format-Table:

$list = "letter","solo","nap","nap","nap","sharp","ignite","tap","tap","tap","tap","evoke"

$groups = $list | Group-Object | Select-Object Name, Count

$psObject = New-Object -TypeName psobject

foreach ($group in $groups) {
    $psObject | Add-Member -NotePropertyName $group.Name -NotePropertyValue $group.Count
}

$psObject | Format-Table

Output:

evoke ignite letter nap sharp solo tap
----- ------ ------ --- ----- ---- ---
    1      1      1   3     1    1   4

One solution would be to put it into an PsObject and then export that object into a CSV:

$list = [System.Collections.ArrayList]@()

$list = "letter","solo","nap","nap","nap","sharp","ignite","tap","tap","tap","tap","evoke"

$hash = $list |  Group-Object | select name,count

$object      = New-Object psobject

foreach( $item in $hash ) {
    $column_name = $item.Name
    $row_value   = $item.Count
    $object | Add-Member -MemberType NoteProperty -Name $column_name -Value $row_value
}

$object | Export-csv 'Path to your CSV' -NoTypeInformation
Related