Hello everyone,

I share with you this PowerShell script that allows you to list all the groups of Azure AD roles as well as theirs members.

  1. Param(
  2. [Parameter(Mandatory = $true)]
  3. [string]$TenantName
  4. )
  5. Connect-AzureAD
  6. $mycoll = @()
  7. $role = Get-AzureADDirectoryRole
  8. foreach ($r in $role) {
  9. $users = (Get-AzureADDirectoryRoleMember -ObjectId $r.ObjectId).UserPrincipalName
  10. foreach ($user in $users) {
  11. $u = Get-AzureADUser -Filter "UserPrincipalName eq '$user'"
  12. $row = New-Object System.Object
  13. $row | Add-Member -MemberType NoteProperty -Name "DirectoryRole" -Value $r.DisplayName
  14. $row | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $u.DisplayName
  15. $row | Add-Member -MemberType NoteProperty -Name "UserPrincipalName" -Value $u.UserPrincipalName
  16. $row | Add-Member -MemberType NoteProperty -Name "UserType" -Value $u.UserType
  17. $mycoll += $row
  18. }
  19. }
  20. $filepath = 'D:\Temp\DirectoryRole'+$TenantName+'.csv'
  21. $mycoll | Export-Csv -Path $filepath -NoTypeInformation -Encoding UTF8 -Delimiter ";"