# Consolidating lines in csv file

**URL:** <https://forums.powershell.org/t/consolidating-lines-in-csv-file/14555>\
**Category:** PowerShell Help\
**Created:** [June 18, 2020, 1:24am UTC](https://forums.powershell.org/t/consolidating-lines-in-csv-file/14555 "2020-06-18T01:24:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![robbiecookie101](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@robbiecookie101](https://forums.powershell.org/u/robbiecookie101)\
**Post date:** [June 18, 2020, 1:24am UTC](https://forums.powershell.org/t/consolidating-lines-in-csv-file/14555/1 "2020-06-18T01:24:24Z")

</div>

Hey guys

I have a large CSV file (approx 15,000 rows) that contains a foldername and a username on each line. My goal is to end up with 1 line per foldername along with all of the usernames.

example file

```
Folder,Username
C:\myfolder1,user1
C:\myfolder1,user2
C:\myfolder1,user3
C:\myfolder2,user1
C:\myfolder2,user2
C:\myfolder3,user1
```

desired file

```
Folder,Username1,Username2,Username3
C:\myfolder1,user1,user2,user3
C:\myfolder2,user1,user2
C:\myfolder3,user1
```

&nbsp;

Here’s my code so far and although it works - it takes forever as i’m constantly rereading $data.

```
$data = import-csv sourcefile.csv
$data = $data | sort Folder

$fullinfo = @()

ForEach($line in $data)
{

$info = new-object -typename PSObject
$CurrentFolder = $line.folder

IF(!($fullinfo | ?{$_.folder -eq $CurrentFolder}))
{

$info | add-member -NotePropertyName Folder -NotePropertyValue $CurrentFolder
$results = ($data | ?{$_.folder -eq $CurrentFolder}).user | sort -Unique
$results | %{$I=0}{$i++ ; $info | add-member -notepropertyname UserName$i -notepropertyvalue $_}
$Fullinfo += $info

}
}

$fullinfo | export-csv output.csv
```

I’m hoping somebody can help make my code more efficient, allowing the entire task to take minutes instead of hours.

&nbsp;

Thank you in advance! 🙂

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 18, 2020, 1:51am UTC](https://forums.powershell.org/t/consolidating-lines-in-csv-file/14555/2 "2020-06-18T01:51:23Z")

</div>

Robbie, welcome to [Powershell.org](http://Powershell.org). Please take a moment and read the very first post on top of the list of this forum: [Read Me Before Posting! You’ll be Glad You Did!](https://powershell.org/forums/topic/read-me-before-posting-youll-be-glad-you-did).

When you post code, error messages, sample data or console output format it as code, please.  
In the “ **Text** ” view you can use the code tags “ **PRE** ”, in the “ **Visual** ” view you can use the format template “ **Preformatted** ”. You can go back edit your post and fix the formatting - you don’t have to create a new one.  
Thanks in advance.

The way you wanted to do would not provide a valid CSV file. Try this:

```
$SourceData = 
@'
Folder,Username
C:\myfolder1,user1
C:\myfolder1,user2
C:\myfolder1,user3
C:\myfolder2,user1
C:\myfolder2,user2
C:\myfolder3,user1
'@ |
ConvertFrom-Csv

$SourceData | 
Group-Object -Property Folder |
ForEach-Object {
    [PSCustomObject]@{
        Folder = $_.Name 
        Users = $_.Group.UserName -join ','
    }
} -OutVariable FullInfo

$FullInfo | Export-Csv -Path output.csv -NoTypeInformation
```

Of course you should provide your source data with an Import-Csv. 😉 … the result would look like this:

```
Folder Users
------ -----
C:\myfolder1 user1,user2,user3
C:\myfolder2 user1,user2
C:\myfolder3 user1
```

---

<div class="post-metadata">

**Author:** ![robbiecookie101](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@robbiecookie101](https://forums.powershell.org/u/robbiecookie101)\
**Post date:** [June 18, 2020, 4:40am UTC](https://forums.powershell.org/t/consolidating-lines-in-csv-file/14555/3 "2020-06-18T04:40:02Z")

</div>

Hey Olaf,

Thank you very much my friend. I have now fixed my original post 🙂

Your code was of great help! I had completely forgotten about the Group-Object cmdlet.

Here’s my new code, it now takes approximately 90 seconds to run!!!

```
$fullinfo = @()

$data = import-csv sourcefile.csv
$Data | Group-Object -Property Folder | 
ForEach-Object {
$info = new-object -typename PSObject
$info | add-member -NotePropertyName Folder -NotePropertyValue $_.name
$_.group.user | sort -Unique | %{$i=0}{$i++;$info | add-member -NotePropertyName user$i -NotePropertyValue $_}

$fullinfo += $info
}

$fullinfo | Export-csv Destfile.csv -NoTypeInformation
```

Thanks again!  
Robbie

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![andysvints](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/andysvints/32/1017_2.png) [@andysvints](https://forums.powershell.org/u/andysvints)\
**Post date:** [June 18, 2020, 7:46pm UTC](https://forums.powershell.org/t/consolidating-lines-in-csv-file/14555/4 "2020-06-18T19:46:17Z")

</div>

Hello Robbie,

If you would change your `$fullinfo` to `ArrayList` (`System.Collections.Arraylist`) and instead of using `+=` would use `ArrayList.Add()` it will be even faster.  
Like this:

```
$FullInfo = New-Object System.Collections.ArrayList
$Data = import-csv sourcefile.csv

$Data | Group-Object -Property Folder | ForEach-Object {
$info = new-object -typename PSObject $info | add-member -NotePropertyName Folder -NotePropertyValue $_.name $_.group.user | sort -Unique | %{$i=0}{$i++;$info | add-member -NotePropertyName user$i -NotePropertyValue $_}
$Fullinfo.Add($info) | out-null
}

$Fullinfo | Export-csv Destfile.csv -NoTypeInformation
```

Hope that helps.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 18, 2020, 9:09pm UTC](https://forums.powershell.org/t/consolidating-lines-in-csv-file/14555/5 "2020-06-18T21:09:41Z")

</div>

> [@](#):
>
> If you would change your $fullinfo to ArrayList (System.Collections.Arraylist) and instead of using += would use ArrayList.Add() it will be even faster.

There’s an even easier way than that:

```
$data = import-csv sourcefile.csv
$fullinfo =
$Data | Group-Object -Property Folder | 
ForEach-Object {
    $info = new-object -typename PSObject
    $info | add-member -NotePropertyName Folder -NotePropertyValue $_.name
    $_.group.user | Sort-Object -Unique | ForEach-Object { $i = 0 } { $i++; $info | add-member -NotePropertyName user$i -NotePropertyValue $_ }
}
$fullinfo | Export-csv Destfile.csv -NoTypeInformation
```

---

<div class="post-metadata">

**Author:** ![robbiecookie101](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@robbiecookie101](https://forums.powershell.org/u/robbiecookie101)\
**Post date:** [June 18, 2020, 10:38pm UTC](https://forums.powershell.org/t/consolidating-lines-in-csv-file/14555/6 "2020-06-18T22:38:30Z")

</div>

Thank you gents, both are great options! 🙂

---

<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:29pm UTC](https://forums.powershell.org/t/consolidating-lines-in-csv-file/14555/7 "2024-05-16T20:29:36Z")

</div>

Closed on May 16, 2024, 8:29pm UTC


