Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

powershell

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

like image 887
Jim Avatar asked Oct 20 '15 12:10

Jim


People also ask

How do you select the first Object in PowerShell?

To select objects from a collection, use the First, Last, Unique, Skip, and Index parameters. To select object properties, use the Property parameter. When you select properties, Select-Object returns new objects that have only the specified properties.

What does group object do in PowerShell?

Group-Object returns a table with one row for each property value and a column that displays the number of items with that value. If you specify more than one property, Group-Object first groups them by the values of the first property, and then, within each property group, it groups by the value of the next property.

Which cmdlet selects the properties of an Object or a group of objects?

The Select-Object cmdlet selects specified properties of an object or set of objects. It can also select unique objects, a specified number of objects, or objects in a specified position in an array. To select objects from a collection, use the First , Last , Unique , Skip , and Index parameters.

Which command is used to extract specific members of each Object?

The [[ operator is used to extract elements of a list or a data frame. It can only be used to extract a single element and the class of the returned object will not necessarily be a list or data frame.


1 Answers

If I understand correctly, this should do it:

$list | %{$_.Group[0].FullName}
like image 89
EBGreen Avatar answered Oct 10 '22 17:10

EBGreen