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.
- Param(
- [Parameter(Mandatory = $true)]
- [string]$TenantName
- )
- Connect-AzureAD
- $mycoll = @()
- $role = Get-AzureADDirectoryRole
- foreach ($r in $role) {
- $users = (Get-AzureADDirectoryRoleMember -ObjectId $r.ObjectId).UserPrincipalName
- foreach ($user in $users) {
- $u = Get-AzureADUser -Filter "UserPrincipalName eq '$user'"
- $row = New-Object System.Object
- $row | Add-Member -MemberType NoteProperty -Name "DirectoryRole" -Value $r.DisplayName
- $row | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $u.DisplayName
- $row | Add-Member -MemberType NoteProperty -Name "UserPrincipalName" -Value $u.UserPrincipalName
- $row | Add-Member -MemberType NoteProperty -Name "UserType" -Value $u.UserType
- $mycoll += $row
- }
- }
- $filepath = 'D:TempDirectoryRole'+$TenantName+'.csv'
- $mycoll | Export-Csv -Path $filepath -NoTypeInformation -Encoding UTF8 -Delimiter ";"