0

I need to get members of all groups which name contains Administrator

  $role = Get-AzureADDirectoryRole | Where-Object {$_.displayName -like '*Administrator'}

Output:

$role.objectid
4ab7edc2-1995-4bdc-9c7d-de7d964e07df
5758628c-b15c-4522-b2d2-14003b7504f1
6131150a-93df-442f-be12-db2313e3308a
b5bd492f-e894-48d9-bfc7-a1e06b4b9e29
b5c1ee12-310f-40ef-9280-d24a2b0b1119
ba3fc590-badf-4f10-b9d3-7d5f76637349
c2c3cf45-9d07-4ee4-b4af-097a4998d92f
ccb0883c-53a8-4c72-a208-0c2fa468d2d4
d919e1b1-74b3-4024-867a-b668a3fa721f
e7677915-5fb1-4317-b583-58dc24365259
f3c465e7-b5b3-4a87-b299-f2db2e5dc003

When running

 foreach ($roles in $role) {Get-AzureADDirectoryRoleMember -objectid $roles.ObjectId | Get-AzureADUser | select displayname,userprincipalname,$roles.DisplayName}

getting

  DisplayName            UserPrincipalName      Helpdesk Administrator
-----------            -----------------      ----------------------
user1                  user1@company.com
user2                  user2@company.com
user3                  user3@company.com

user3 is member of multiple groups with Administrator in name.It list only members of first group with Administrator in name (Helpdesk Administrator)

  • 2
    After your last edit you've left out an actual question; – Lieven Keersmaekers Jul 12 '18 at 18:39
1

For clarity I'd adjust singular/pural with $role(s)

$roles = Get-AzureADDirectoryRole | Where-Object {$_.DisplayName -like '*Administrator'}
$UserRoles = ForEach ($role in $roles) {
    Get-AzureADDirectoryRoleMember -ObjectId "$($role.ObjectId)" | Get-AzureADUser | 
    ForEach-Object {
        [PSCustomObject]@{
             User             = $_.DisplaName
             UserPincipalName = $_.UserPincipalName
             AdministratorRole= $role.DisplayName
        }
    }
}

You could then Group-Object on User or UserPrincipalName and join the AdministratorRoles in one property.

| improve this answer | |
  • thanks @LotPings, i already getting same output, i need to show groupname for each user – Milister Jul 12 '18 at 18:48
  • See changed script. – LotPings Jul 12 '18 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.