Search code examples
groupingpowershell-4.0

How to loop through results of Group-Object


I love what Group-Object can do, but I don't know how to access what it returns. I want to write a unique list of URLs to a file (nothing else), sorted by the highest count first. Not even sure I need the -AsHashTable or -AsString, tried with and without.

$urls = @() 
$urls += "http://example.com/test.aspx?a1"
$urls += "http://example.com/test.aspx?b2"
$urls += "http://example.com/test.aspx?c3"
$urls += "http://example.com/test.aspx?a1"
$urls += "http://example.com/test.aspx?b2"
$urls += "http://example.com/test.aspx?a1"
$urls += "http://example.com/test.aspx?a1"

Write-Host "Group & Count"
$urls | Group-Object
#the above is perfect, I just access to the individual fields there 

Write-Host "`n`nSummary:"

$summaryUrls = Group-Object -InputObject $urls  #| Select Name, Count | Format-Table -Auto 
$summaryUrls 

Write-Host "`n`nLoop:"
foreach ($summaryUrl in $summaryUrls) {
    Write-Host "Name=$($summaryUrl.Name) Count=$($summaryUrl.Count)"
    #could build new array here to write to file but loop isn't doing anything 
}

Output:

Group & Count

Count Name                      Group                                                                                                                                       
----- ----                      -----                                                                                                                                       
    4 http://example.com/tes... {http://example.com/test.aspx?a1, http://example.com/test.aspx?a1, http://example.com/test.aspx?a1, http://example.com/test.aspx?a1}        
    2 http://example.com/tes... {http://example.com/test.aspx?b2, http://example.com/test.aspx?b2}                                                                          
    1 http://example.com/tes... {http://example.com/test.aspx?c3}                                                                                                           


Summary:
    1 http://example.com/tes... {http://example.com/test.aspx?a1 http://example.com/test.aspx?b2 http://example.com/test.aspx?c3 http://example.com/test.aspx?a1 http://e...


Loop:
http://example.com/test.aspx?a1 http://example.com/test.aspx?b2 http://example.com/test.aspx?c3 http://example.com/test.aspx?a1 http://example.com/test.aspx?b2 http://exampl
e.com/test.aspx?a1 http://example.com/test.aspx?a1 1

PowerShell Version: 5.1.17763.592


Solution

  • From https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/group-object?view=powershell-6

    -InputObject

    Specifies the objects to group. Enter a variable that contains the objects, or type a command or expression that gets the objects.

    When you use the InputObject parameter to submit a collection of objects to Group-Object, Group-Object receives one object that represents the collection. As a result, it creates a single group with that object as its member.

    To group the objects in a collection, pipe the objects to Group-Object.

    So you need to use the version with the pipe in your assignment:

    $summaryUrls = $urls | Group-Object