# ForEach Trouble - I'm sure I'm Missing Something Small

**URL:** <https://forums.powershell.org/t/foreach-trouble-im-sure-im-missing-something-small/14592>\
**Category:** PowerShell Help\
**Created:** [June 26, 2020, 5:00pm UTC](https://forums.powershell.org/t/foreach-trouble-im-sure-im-missing-something-small/14592 "2020-06-26T17:00:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpm](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rpm/32/1336_2.png) [@rpm](https://forums.powershell.org/u/rpm)\
**Post date:** [June 26, 2020, 5:00pm UTC](https://forums.powershell.org/t/foreach-trouble-im-sure-im-missing-something-small/14592/1 "2020-06-26T17:00:44Z")

</div>

I know what I’m doing wrong, but I don’t know how to code myself out of the problem.

I am trying to get all of the users for a specific Distribution Group, but I’m only using one variable. Can someone help get me out of this paper bag I’ve put over my head?

Thanks,

-Rob

```
$SharedMailboxes = @()
$Rollup = @()
$i = 0

$SharedMailboxes = Get-EXORecipient -ResultSize Unlimited -Filter {(EmailAddresses -like ‘_@domain1.de’) -or (EmailAddresses -like '_@domain2.co.uk’) -or (EmailAddresses -like ‘_@domain3.com’) -or (EmailAddresses -like '_@domain4.de’)} | ? RecipientTypeDetails -eq “SharedMailbox”  
ForEach ($SharedMailbox in $SharedMailboxes){  
$MailboxUsers = Get-EXOMailboxPermission $SharedMailbox.Identity |? user -like ‘_@_’  
ForEach ($MailboxUser in $MailboxUsers){  
$Name = Get-EXORecipient -Identity $MailboxUser.User  
}  
$item = New-Object PSObject  
$item | Add-Member -Type NoteProperty -Name ‘Distribution Group’ -Value $SharedMailbox.DisplayName  
$item | Add-Member -Type NoteProperty -Name ‘Distribution Group Email’ -Value $SharedMailbox.PrimarySMTPAddress  
$item | Add-Member -Type NoteProperty -Name ‘UPN’ -Value $Name.DisplayName  
$item | Add-Member -Type NoteProperty -Name ‘Primary Email’ -Value $Name.PrimarySMTPAddress  
$Rollup += $item  
$i++  
Write-Progress -activity “Researching Distro” -status “Distro: $i of $($SharedMailboxes.Count)” -percentComplete (($i / $SharedMailboxes.Count) \* 100)  
}
```

&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 26, 2020, 7:40pm UTC](https://forums.powershell.org/t/foreach-trouble-im-sure-im-missing-something-small/14592/2 "2020-06-26T19:40:44Z")

</div>

Hello R\_Martin,  
You are closing your nested foreach loop to soon. Simply move closing bracket “`}`” from line #10 to line #20.

Also I noticed that you are using `+=` operator to dynamically add objects to `$Rollup` array. `+=` Operator is not best approach as it is very time consuming. And during execution of your script your Office365 session might be dropped and script will fail. I would suggest to use **ArrayList**.  
Substitute line #2 to following

```
$Rollup=New-Object System.Collections.ArrayList
```

and line #16 to

```
$Rollup.Add($item)
```

Hope that helps.

---

<div class="post-metadata">

**Author:** ![rpm](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rpm/32/1336_2.png) [@rpm](https://forums.powershell.org/u/rpm)\
**Post date:** [June 27, 2020, 3:59am UTC](https://forums.powershell.org/t/foreach-trouble-im-sure-im-missing-something-small/14592/3 "2020-06-27T03:59:00Z")

</div>

Hello Andy!

Thank you for your help. It works perfectly now. The only thing I added was on line 16 I did the following to keep it from counting up on screen.

```
$Rollup.Add($item) > $Null
```

Thanks again!

---

<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 27, 2020, 11:31am UTC](https://forums.powershell.org/t/foreach-trouble-im-sure-im-missing-something-small/14592/4 "2020-06-27T11:31:37Z")

</div>

Glad that you figured it out.

More PowerShellish way would be to pipe it to `Out-Null`

=)

Hope that helps.

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [June 28, 2020, 3:11am UTC](https://forums.powershell.org/t/foreach-trouble-im-sure-im-missing-something-small/14592/5 "2020-06-28T03:11:02Z")

</div>

Where do those red spaces come from again (non-breaking spaces or 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/foreach-trouble-im-sure-im-missing-something-small/14592/6 "2024-05-16T20:29:19Z")

</div>

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


