# Getting hashtable in for each loop

**URL:** <https://forums.powershell.org/t/getting-hashtable-in-for-each-loop/21228>\
**Category:** PowerShell Help\
**Created:** [December 25, 2022, 11:11am UTC](https://forums.powershell.org/t/getting-hashtable-in-for-each-loop/21228 "2022-12-25T11:11:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saeed\_Ahmad](https://avatars.discourse-cdn.com/v4/letter/s/8e7dd6/32.png) [@Saeed\_Ahmad](https://forums.powershell.org/u/Saeed_Ahmad)\
**Post date:** [December 25, 2022, 11:11am UTC](https://forums.powershell.org/t/getting-hashtable-in-for-each-loop/21228/1 "2022-12-25T11:11:05Z")

</div>

HI there, I m trying to get list of AZURE privilage Groups, their members and other group properties.  
I use below script, but when I check $properties variable, it store only one entry.  
#Getting list of AZURE RoleGroups  
$RoleGroups = Get-AzureADMSPrivilegedResource -ProviderId “aadGroups”

$Assignment = @()

foreach($g in $RoleGroups){  
$Assignment += Get-AzureADMSPrivilegedRoleAssignment -ProviderId “aadGroups” -ResourceId $g.id  
}

$result = @()  
foreach($c in $Assignment){

$properties = @{GroupName = ‘(Get-AzureADGroup -ObjectId $c.ResourceId).DisplayName’  
AdminName = (Get-AzureADUser -ObjectId $c.SubjectId ).UserPrincipalName  
Assignment = $c.AssignmentState  
MemberType = $c.MemberType  
StartDate = $c.StartDateTime  
EndDate = $c.EndDateTime  
}

}

My proble is that script runs OK but when I run $properties it has only one entry in it.  
Many thanks for the help  
Sa

---

<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:** [December 25, 2022, 11:36am UTC](https://forums.powershell.org/t/getting-hashtable-in-for-each-loop/21228/2 "2022-12-25T11:36:00Z")

</div>

Saeed,  
Welcome to the forum. 👋🏽

When you post code, sample data, console output or error messages please format it as code using the _preformatted text_ button ( **\</\>** ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

[How to format code in PowerShell.org](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

> [@Saeed\_Ahmad](#):
>
> it store only one entry.

You overwrite the variable `$properties` in each loop iteration. That’s why you only have the last one in it when you finish the loop.

This should do the trick:

```auto
$RoleGroups = Get-AzureADMSPrivilegedResource -ProviderId 'aadGroups'

$Assignment =
foreach ($g in $RoleGroups) {
    Get-AzureADMSPrivilegedRoleAssignment -ProviderId 'aadGroups' -ResourceId $g.id
}

$result = 
foreach ($c in $Assignment) {
    [PSCustomObject]@{
        GroupName = (Get-AzureADGroup -ObjectId $c.ResourceId).DisplayName
        AdminName = (Get-AzureADUser -ObjectId $c.SubjectId ).UserPrincipalName
        Assignment = $c.AssignmentState
        MemberType = $c.MemberType
        StartDate = $c.StartDateTime
        EndDate = $c.EndDateTime
    }
}

$result 

```

---

<div class="post-metadata">

**Author:** ![Saeed\_Ahmad](https://avatars.discourse-cdn.com/v4/letter/s/8e7dd6/32.png) [@Saeed\_Ahmad](https://forums.powershell.org/u/Saeed_Ahmad)\
**Post date:** [December 25, 2022, 1:29pm UTC](https://forums.powershell.org/t/getting-hashtable-in-for-each-loop/21228/3 "2022-12-25T13:29:53Z")

</div>

Such a fabulous forum. Many thanks Olaf, worked like a charm. Greatly apprecate your help.  
Best regards  
Sa

---

<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:16pm UTC](https://forums.powershell.org/t/getting-hashtable-in-for-each-loop/21228/4 "2024-05-16T20:16:11Z")

</div>

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


