Search code examples
arrayspowershellcomparison

How to sort array for already existing names and add objects from those names to original name


I currently have an array in Powershell giving me a list of users with certain objects (info). There are multiple instances of certain users but with different info attached. I want to add that info so when I search for a certain element/name in array it comes back with all the info.

So say for example I have three array.name all equal to John. But each instance has different addresses. I want to add those different address together so I have one array.name equal to John but it has the three different address attached.

I have tried a loop within a loop and creating a new array by trying to compare what I already have with the new array. But it doesn't work and spews out an endless list.

$arr3 += $arr2[0]

For ($a=0; $a -le ($arr2.length - 1); $a++) {
    $temp3 = $arr2[$a].managedBy
    if ($temp3 -eq $null){
        $temp3 = "none"
    }
    For ($b=0; $b -le ($arr3.length - 1); $b++) {
        if ($temp3 -eq $arr3[$b].managedBy) {
            $arr3[$b].name += "`n"
            $arr3[$b].name += $arr2[$a].name
            $arr3[$b].description += "`n"
            $arr3[$b].description += $arr2[$a].description
            $arr3[$b].info += "`n"
            $arr3[$b].info += $arr2[$a].info
        } else {
            $arr3 += $arr2[$a]
        }
    }
}

I should have arr3 with a list of managedBy that don't repeat but with extra info attached in some cases. This doesn't work and ends up spewing out a huge endless array.


Solution

  • Applying Lee_Daily's suggestions, you can do the following:

    $arr3 = $arr2 | Group-Object ManagedBy |
       select @{n='ManagedBy';e={$_.Name}},@{n='Name';e={$_.group.Name -join ";"}},
       @{n='description';e={$_.group.description -join ";"}},
       @{n='info';e={$_.group.info -join ";"}}
    

    This code groups names that are identical into one line of output using Group-Object. The .Group property contains the remaining properties of the $arr2 objects. All of the array object properties are stored in the .group property of the Group-Object output, hence the need to use nested properties ($_.group.property) to access them. I excluded ManagedBy. I use the -join operator to join all values of a single property with a semi-colon delimiter.

    To customize this code for your situation, you can change the following since you may need to swap properties to reach your desired effect:

    1. Group-Object ManagedBy: You can use another property besides ManagedBy. This should be the property that contains duplicate values across your array that you want to reduce to one entry.
    2. Select @{n=...;e={...}}: The property you are grouping by (ManagedBy in this case) is mapped to the .name property of the Group-Object output. The remaining headers and values in the hash table should be the multiple values you want to join in one line. The hash table values should exclude the grouped property.
    3. -join ";": This is the delimiter that separates your common properties' values. Your info property value will be in the format Info 1;Info 2;Info 3. You can update this delimiter.