# how to iterate over all elements of each group in powershell

**URL:** <https://forums.powershell.org/t/how-to-iterate-over-all-elements-of-each-group-in-powershell/14497>\
**Category:** PowerShell Help\
**Created:** [June 6, 2020, 5:06pm UTC](https://forums.powershell.org/t/how-to-iterate-over-all-elements-of-each-group-in-powershell/14497 "2020-06-06T17:06:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pallavik](https://avatars.discourse-cdn.com/v4/letter/p/58956e/32.png) [@pallavik](https://forums.powershell.org/u/pallavik)\
**Post date:** [June 6, 2020, 5:06pm UTC](https://forums.powershell.org/t/how-to-iterate-over-all-elements-of-each-group-in-powershell/14497/1 "2020-06-06T17:06:46Z")

</div>

I have a input csv file as below

| Loginname | AtlasOrgname | Projectname | Dbname | roleName |
| usera | organisation1 | project1 | marketing | read |
| usera | organisation1 | project1 | sales | read |
| userb | organisation1 | project2 | sales | read |
| userb | organisation1 | project2 | marketing | read |

I have created groups as below, code is as below
```
$inputfile = Import-Csv -path $csvfile
$groups=$inputfile|Group-Object loginname,Projectname,AtlasOrgname|Select-Object @{n='loginname';e={$_.Group[0].loginname}},
@{n='Projectname';e={$_.Group[0].Projectname}},@{n='AtlasOrgname';e={$_.Group[0].AtlasOrgname}},
@{n='Dbname';e={$_.Group[0].Dbname}},@{n='roleName';e={$_.Group[0].roleName}},Count 
$groups | ForEach-Object { 
  $dbname = $_.Dbname
  $usrname = $_.loginname
  $prjname=$_.Projectname
  $dbname=$_.Dbname
  $rolename=$_.rolename
   write-Host "databaseName:" $dbname "," "roleName:" $rolename "," "projectname" $prjname "," "username" $usrname
   }
```

In the Output , i am able to see only first record of each group , how can i loop over all records in each group

To be more specific,Inside each group ,How can i iterate over each record of each group

My plan is to use Invoke-RestMethod for each record in each group and then after processing all records in a group ,i want to use Send-MailMessage function at the end of each group instead of each record 

can some one please help , how can i iterate over each record in each group , Thank you

---

<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 6, 2020, 5:21pm UTC](https://forums.powershell.org/t/how-to-iterate-over-all-elements-of-each-group-in-powershell/14497/2 "2020-06-06T17:21:43Z")

</div>

pallavi

Please, 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.

What would be the desired output? Why don’t you simply iterate over all elements in input file?

---

<div class="post-metadata">

**Author:** ![michael-orellana-54](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@michael-orellana-54](https://forums.powershell.org/u/michael-orellana-54)\
**Post date:** [June 8, 2020, 11:00am UTC](https://forums.powershell.org/t/how-to-iterate-over-all-elements-of-each-group-in-powershell/14497/3 "2020-06-08T11:00:26Z")

</div>

When Powershell imports your file on line #1 it creates an object of your values in your sheet so you can just iterate over that variable(object).

`$inputfile = Import-Csv -path $csvfile`

`Foreach($row in $inputFile){`  
`$row`  
`}`

&nbsp;

---

<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/how-to-iterate-over-all-elements-of-each-group-in-powershell/14497/4 "2024-05-16T20:29:23Z")

</div>

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


