Group ArrayList of Custom Objects by Property

I would like to group $mya‘s objects based on bytes:

Name           Bytes
----           -----
.\filedata.py  {105, 109, 112, 111...}
.\testfile.txt {100, 102, 115, 97...}
.\copyfile.txt {100, 102, 115, 97...}

I’ve tried

$mya | Group-Object -Property Bytes

But the result I get is strange:

Count Name                      Group
----- ----                      -----
    3 {105, 109, 112, 111, 1... {@{Name=.\filedata.py; Bytes=System.Object[]}, @{Name=.\testfile.txt; Byte...

I don’t understand why I got this result. I would have thought that the pipeline unwrapped all objects from $mya and grouped them based on their requested property, Bytes.

My desired outcome is:

Count Name                        Group
----- ----                        -----
    1 {105, 109, 112, 111, ...}   {\.filedata.py}
    2 {100, 102, 115, 97, ...}    {\.testfile.txt, .\copyfile.txt}

>Solution :

Group-Object can’t quite figure out what to do with your byte array values.

Use an anonymous property expression to create something Group-Object can meaningfully compare – like strings – from the byte arrays:

$mya | Group-Object -Property { [BitConverter]::ToString($_.Bytes) }

Leave a Reply