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
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.
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.
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.
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.
If I understand correctly, this should do it:
$list | %{$_.Group[0].FullName}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With