# Nested ForEach

**URL:** <https://forums.powershell.org/t/nested-foreach/9897>\
**Category:** PowerShell Help\
**Created:** [December 22, 2017, 8:39am UTC](https://forums.powershell.org/t/nested-foreach/9897 "2017-12-22T08:39:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![prisoner10](https://avatars.discourse-cdn.com/v4/letter/p/bbce88/32.png) [@prisoner10](https://forums.powershell.org/u/prisoner10)\
**Post date:** [December 22, 2017, 8:39am UTC](https://forums.powershell.org/t/nested-foreach/9897/1 "2017-12-22T08:39:19Z")

</div>

Can someone please tell me what I am doing wrong with the below code.  
Im having trouble pulling the “LastLogontime” Attribute from the Get-MailboxStatistics cmdlet, and its duplicating all the data. Im using the Invoke-All for Speed on the Get-MailboxStatics, since its usually a slow cmdlet.  
I’ve changed the code several times and wasted too many hours trying to make it work, i’ve no other choice but to ask for help.  
How can I make this an efficient script?

```
$AzureUser = Get-AzureADUser -All $true | Where-Object { $_.DisplayName -match 'jose' } 
$mb = $AzureUser |Invoke-All {Get-MailboxStatistics -Identity $_.DisplayName}

  & {
    foreach ($mbx in $mb)
    {
    foreach ($Azure in $AzureUser) 
    {
            [pscustomobject] @{
                  DisplayName = $Azure.DisplayName
                  UserPrincipalName = $Azure.UserPrincipalName
                  LastDirSyncTime = $Azure.LastDirSyncTime
                  EmployeeId = $Azure.ExtensionProperty["employeeId"]
                  Licensed = if($Azure.AssignedLicenses){$TRUE}else{$False}
                  Plan = if($Azure.AssignedPlans){$TRUE}else{$False}
                  Mail = $Azure.Mail
                  AccountEnabled = $Azure.AccountEnabled
                  SamAccountName = ($Azure.DisplayName | foreach {Get-ADUser -filter {Displayname -eq $_}}).SamAccountName
                  LastLogon = $mbx.LastLogontime
                  }
            }
        }
} | Export-Csv Desktop:\NewTest.csv -NoTypeInformation
```

---

<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:** [December 22, 2017, 8:57am UTC](https://forums.powershell.org/t/nested-foreach/9897/2 "2017-12-22T08:57:18Z")

</div>

Can’t pipe from that kind of foreach.

---

<div class="post-metadata">

**Author:** ![sanchez](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@sanchez](https://forums.powershell.org/u/sanchez)\
**Post date:** [December 22, 2017, 10:11am UTC](https://forums.powershell.org/t/nested-foreach/9897/3 "2017-12-22T10:11:03Z")

</div>

I ran the code in my environment and it duplicates the data as well, honestly I have no clue why. But to get the lastlogontime I did this

```
$mb = $AzureUser | % { Get-MailboxStatistics -Identity $_.displayname }
```

---

<div class="post-metadata">

**Author:** ![prisoner10](https://avatars.discourse-cdn.com/v4/letter/p/bbce88/32.png) [@prisoner10](https://forums.powershell.org/u/prisoner10)\
**Post date:** [December 22, 2017, 10:14am UTC](https://forums.powershell.org/t/nested-foreach/9897/4 "2017-12-22T10:14:35Z")

</div>

Correct, if I drop the Invoke-All Function from the script it will work as expected. Was just trying to find a way to multithread at the same time. Might not be possible like js mentioned.

---

<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:** [December 22, 2017, 10:17am UTC](https://forums.powershell.org/t/nested-foreach/9897/5 "2017-12-22T10:17:30Z")

</div>

How about this way…

```
$mb | foreach 
  {
  $mbx = $_
  $AzureUser | foreach 
  {
    $Azure = $_
```

---

<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:36pm UTC](https://forums.powershell.org/t/nested-foreach/9897/6 "2024-05-16T20:36:57Z")

</div>


