Jim
Jim

Reputation: 16022

Group-Object in powershell, select first item in each group

Given the following script.

$module = # get path
$code = get-childitem $module -recurse -include *.dll
$list = $code | group-object -Property Name
$list

I retreive the following:

Count Name                      Group                                                                                                                                                         
----- ----                      -----                                                                                                                                                         
    4 FileA...                  {A, B, C, D}
    2 FileB...                  {X, Z}
    1 FileX...                  {R}

How do I select the first item in each group.
I want to group by the file name, but actually retrieve the first file (full path) in each group.

I want to obtain A,X,R etc

Upvotes: 6

Views: 8697

Answers (1)

EBGreen
EBGreen

Reputation: 37730

If I understand correctly, this should do it:

$list | %{$_.Group[0].FullName}

Upvotes: 13

Related Questions