# help with parsing csv

**URL:** <https://forums.powershell.org/t/help-with-parsing-csv/6148>\
**Category:** PowerShell Help\
**Created:** [April 6, 2016, 8:27pm UTC](https://forums.powershell.org/t/help-with-parsing-csv/6148 "2016-04-06T20:27:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![voytek-szaro](https://avatars.discourse-cdn.com/v4/letter/v/919ad9/32.png) [@voytek-szaro](https://forums.powershell.org/u/voytek-szaro)\
**Post date:** [April 6, 2016, 8:27pm UTC](https://forums.powershell.org/t/help-with-parsing-csv/6148/1 "2016-04-06T20:27:07Z")

</div>

So i have this a csv file, similar to one below  
user,phonenumber,emailaddress  
[user1,3219873452,user1@email.com](mailto:user1,3219873452,user1@email.com)  
[user2,7651239876,user2@email.com](mailto:user2,7651239876,user2@email.com)  
[user1,3211234321,user1a@email.com](mailto:user1,3211234321,user1a@email.com)  
[user2,7651233214,user2a@email.com](mailto:user2,7651233214,user2a@email.com)  
I would like to be able to get an output for each user with both of their phone numbers and email addresses - just once. So a little story behind this, someone from my company would like to send a single email with the info that’s in the csv file. I’ve tried foreach user but that’s sending multiple emails. Is there a way to send just one message containing both email addresses and phone numbers?  
I don’t need help with send-mailmessage, just getting this damn data in a single email (per user) is giving me a headache!  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [April 6, 2016, 9:12pm UTC](https://forums.powershell.org/t/help-with-parsing-csv/6148/2 "2016-04-06T21:12:52Z")

</div>

What have you tried so far? Can you show some code you have tried?

---

<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:** [April 7, 2016, 12:38am UTC](https://forums.powershell.org/t/help-with-parsing-csv/6148/3 "2016-04-07T00:38:44Z")

</div>

Something like this should work for you.

```
Import-Csv .\test.csv | Group-Object -Property user | 
ForEach-Object {$_.group ; Send-MailMessage -Body $($_.group) -Subject $($_.name)}
```

Below is a working example.

```
$csvoutput = "user,phonenumber,emailaddress
user1,3219873452,user1@email.com
user2,7651239876,user2@email.com
user1,3211234321,user1a@email.com
user2,7651233214,user2a@email.com
" -split "`n"

$csvoutput | ConvertFrom-Csv | Group-Object -Property user | 
ForEach-Object {$_.group}
```

---

<div class="post-metadata">

**Author:** ![voytek-szaro](https://avatars.discourse-cdn.com/v4/letter/v/919ad9/32.png) [@voytek-szaro](https://forums.powershell.org/u/voytek-szaro)\
**Post date:** [April 19, 2016, 3:22am UTC](https://forums.powershell.org/t/help-with-parsing-csv/6148/4 "2016-04-19T03:22:42Z")

</div>

random commandline, thank you, perhaps i didn’t explain it in right.  
I’m looking for something that would look like this:

user1 3219873452 [user1@email.com](mailto:user1@email.com)  
3211234321 [user1a@email.com](mailto:user1a@email.com)  
user2 7651239876 [user2@email.com](mailto:user2@email.com)  
7651233214 [user2a@email.com](mailto:user2a@email.com)  
Thanks!

---

<div class="post-metadata">

**Author:** ![bobmccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bobmccoy/32/650_2.png) [@bobmccoy](https://forums.powershell.org/u/bobmccoy)\
**Post date:** [April 20, 2016, 12:57am UTC](https://forums.powershell.org/t/help-with-parsing-csv/6148/5 "2016-04-20T00:57:05Z")

</div>

```
# create empty output file (no leftovers from previous runs)
$outFile = New-Item -ItemType File -Path . -Name test.txt -Force
# read in the input list and create a group collection based on user
$users = Import-Csv -Path .\test.csv | Group-Object -Property user
foreach ($user in $users)
{
    # first (default) entry per user
    $out = "$($user.Group[0].user) $($user.Group[0].phonenumber) $($user.Group[0].emailaddress)"
    # append additional shortened entries for the same user
    for ($i = 1; $i -le $user.Count; $i++)
    {
        $out = "$out`n$($user.Group[$i].phonenumber) $($user.Group[$i].emailaddress)".TrimEnd()
    }
    # update the output file
    Add-Content -Path $outFile -Value $out
}
```

If there will never be more than two entries then the for loop can be replaced with a simple ‘if’ statement and the index ($i) hardcoded to 1. But this version will work with any number of entries per user.

---

<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:43pm UTC](https://forums.powershell.org/t/help-with-parsing-csv/6148/6 "2024-05-16T20:43:14Z")

</div>


