Hello @Glenn Maxwell,
Thank you for posting your query on Microsoft Q&A Forum. As per your query, I understand that you wanted to export the list of all users in your tenant including their assigned roles.
I tried to reproduce the same in my tenant and I had successfully exported the list of all users, UPN and Assigned roles into an excel sheet.
Please find the steps below-
Open powershell as an admin and run the below command-
Install-Module Microsoft.Graph -scope CurrentUser
Once you execute the above command Accept all the prompts you get to install the module. It would take upto 5 minutes to install this module and once the module is executed run the below command-
Connect-MGGraph -Scopes "User.Read.All", "Directory.Read.All", "RoleManagement.Read.Directory"
During this process, you would get a Microsoft Login prompt where you need to enter your Global Admin credentials and complete the Authentication. Once this is done, please run the below command at once-
================================================================
# Get all users
$users = Get-MgUser -All
# Get all directory role assignments
$roleAssignments = Get-MgDirectoryRole -All | ForEach-Object {
$role = $_
Get-MgDirectoryRoleMember -DirectoryRoleId $role.Id | ForEach-Object {
[PSCustomObject]@{
UserId = $_.Id
UserName = $_.UserPrincipalName
RoleName = $role.DisplayName
}
}
}
# Join user info with role info
$usersWithRoles = foreach ($user in $users) {
$userRoles = $roleAssignments | Where-Object { $_.UserId -eq $user.Id }
if ($userRoles) {
foreach ($role in $userRoles) {
[PSCustomObject]@{
DisplayName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
Role = $role.RoleName
}
}
} else {
[PSCustomObject]@{
DisplayName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
Role = "None"
}
}
}
# Export to CSV
$usersWithRoles | Export-Csv -Path "C:\DesiredName.csv" -NoTypeInformation
================================================================
So, in the given Export Path in the above command, the CSV file with list of all users, UPN and Roles would be populated and if any user does not have any role it would populate as None.
I hope this information is helpful. Please feel free to reach out if you have any further questions.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".