Skip to content

Instantly share code, notes, and snippets.

@MatthewJDavis
Last active July 6, 2021 23:00
Show Gist options
  • Save MatthewJDavis/6b385187cde51b26ac2a03a84d619835 to your computer and use it in GitHub Desktop.
Save MatthewJDavis/6b385187cde51b26ac2a03a84d619835 to your computer and use it in GitHub Desktop.
Azure AD priv roles
# Script to get all User Principal Names of users in the Global Administrators role in Azure Active Directory.
# Uses the MSGraph beta endpoint and requires the correct permissions to access the data. See: https://docs.microsoft.com/en-us/graph/api/rbacapplication-list-roledefinitions?view=graph-rest-beta&tabs=http#permissions.
Select-MgProfile -Name "beta"
Connect-MgGraph -Scopes 'RoleManagement.Read.Directory'
$memberList = [System.Collections.Generic.List[string]]::new()
$roleId = (Get-MgDirectoryRole -Filter "DisplayName eq 'Global Administrator'").Id
$userList = Get-MgDirectoryRoleMember -DirectoryRoleId $roleId
foreach ($user in $userList) {
$upn = (Get-MgUser -UserId $user.id).UserPrincipalName
$memberList.Add($upn)
}
$memberList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment