# Multivalued Array - concatenate double values

**URL:** <https://forums.powershell.org/t/multivalued-array-concatenate-double-values/15376>\
**Category:** PowerShell Help\
**Created:** [December 11, 2020, 5:43am UTC](https://forums.powershell.org/t/multivalued-array-concatenate-double-values/15376 "2020-12-11T05:43:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhn](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@jhn](https://forums.powershell.org/u/jhn)\
**Post date:** [December 11, 2020, 5:43am UTC](https://forums.powershell.org/t/multivalued-array-concatenate-double-values/15376/1 "2020-12-11T05:43:59Z")

</div>

Hello, been bugging me all day and I cannot seem to find a decent solution … I hope the powershell community can help me.

I have the following array:

| **col1** | **Col2** |
| ab | data1 |
| cd | data2 |
| ef | data1 |
| ab | data2 |
| cd | data3 |

The array is created from a csv which is imported through 'Import-CSV' with _-Delimiter ","_ and _-Header "Col1","Col2"_. The goal is to rework this Array to concatenate the value from 'Col2' for the double entries in 'Col1', preferably into a new array. The output would then look like

| **Col1** | **Col2** |
| ab | data1 | data2 |
| cd | data2 | data3 |
| ef | data1 |

I've been fuzzing around with a lot of code snippets but nothing that gives me the desired output.

&nbsp;

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [December 11, 2020, 6:46am UTC](https://forums.powershell.org/t/multivalued-array-concatenate-double-values/15376/2 "2020-12-11T06:46:05Z")

</div>

If it’s only two columns, you could import the CSV into a hashtable and see if the key (column 1) already exists, if it does, add it to existing key (hashtables can’t contain duplicate keys).

&nbsp;

```
$data = @"
col1,col2
ab,data1
cd,data2
ef,data1
ab,data2
cd,data3
"@

$csv = ConvertFrom-Csv $data

$HashTable = @{}

foreach($row in $csv) {

    if ($row.col1 -in $hashTable.Keys) {
    
        $hashTable[$row.col1] = $hashTable[$row.col1] + " | $($row.col2)"
    
    }

    else {

        $hashTable[$row.col1] = $row.col2

    }
    
}

$hashTable

Output:

Name Value
---- -----
cd data2 | data3
ef data1
ab data1 | data2
```

---

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [December 11, 2020, 6:58am UTC](https://forums.powershell.org/t/multivalued-array-concatenate-double-values/15376/3 "2020-12-11T06:58:37Z")

</div>

```
$group = Import-Csv -Path input.csv -Delimiter ',' -Header 'Col1','Col2' | Group-Object -Property Col1
$result = foreach ($g in $group){
    [PSCustomObject]@{
        Col1 = $g.Name
        Col2 = $g.Group.Col2 | Out-String
    }
}
$result | Export-Csv -Path result.csv -NoTypeInformation
```

---

<div class="post-metadata">

**Author:** ![jhn](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@jhn](https://forums.powershell.org/u/jhn)\
**Post date:** [December 15, 2020, 12:07am UTC](https://forums.powershell.org/t/multivalued-array-concatenate-double-values/15376/4 "2020-12-15T00:07:59Z")

</div>

Matt, thank you for your reply. I was also looking at that solution, but that didn’t do the trick.

I must say I forgot to mention there are more then 2 columns.

Finally, the code from random commandline did the trick.

A user, identified by his login, can be multiple times in the received csv file. Each time with a different group.  
Goal was to create a new Array with unique logins (no doubles) with all the groups for the user grouped in one Array element for that login  
Finally, it looks like this:

```
$csv = Import-CSV "received.csv" -Header "Login","Mail","FirstName","LastName","ADGroupId" -Delimiter ";" | Group-Object -Property Login
$result = foreach ($line in $csv)
{
$myvar = $line.Group.ADGroupId | Out-String
$myvar = $myvar -Replace('[\n]', "|")

$NewLine = [PSCustomObject]@{
Login = $line.Name
Mail = $($line.Group.Mail |Select-Object -Last 1)
FirstName = $($line.Group.FirstName |Select-Object -Last 1)
LastName = $($line.Group.LastName |Select-Object -Last 1)
ADGroupId = $myvar.TrimEnd("|")
}
$FlattenArray += $NewLine
}
```

The Out-String comes with a “new line”, hence “_-Replace(‘[\n]’, “|”)_”  
“_TrimEnd(”|“)_” to remove the final ‘|’

Thank you!

---

<div class="post-metadata">

**Author:** ![jhn](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@jhn](https://forums.powershell.org/u/jhn)\
**Post date:** [December 15, 2020, 2:06am UTC](https://forums.powershell.org/t/multivalued-array-concatenate-double-values/15376/5 "2020-12-15T02:06:52Z")

</div>

thank you for your input.  
Matt, i was already trying this piece of code, but it didn’t do the trick. I forgot to mention in my original post that there are more then 2 columns.  
Finally, the code from Random Commandline did the trick.

Input is a csv with several columns, one containing group info.  
The csv can contain multiple lines for the same user, each representing a different group.  
Goal was to create a final array, only with all the unique users with a ‘group’ field per user containing all the groups (if there are multiple).

The final code looks like this:

```
$csv = Import-CSV "received.csv" -Header "Login","Mail","FirstName","LastName","ADGroupId" -Delimiter ";" | Group-Object -Property Login
$result = foreach ($line in $csv)
{
$myvar = $line.Group.ADGroupId | Out-String
$myvar = $myvar -Replace('[\n]', "|")

$NewLine = [PSCustomObject]@{
Login = $line.Name
Mail = $($line.Group.Mail |Select-Object -Last 1)
FirstName = $($line.Group.FirstName |Select-Object -Last 1)
LastName = $($line.Group.LastName |Select-Object -Last 1)
ADGroupId = $myvar.TrimEnd("|")
}
$FlattenArray += $NewLine
}
```

-Replace(‘[\n]’, “|”) –\> the outstring comes with a new line for each value  
.TrimEnd(“|”) –\> to remove the final “|”

Thank you all for your help.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/multivalued-array-concatenate-double-values/15376/6 "2024-05-16T20:28:10Z")

</div>


