Skip to content

Instantly share code, notes, and snippets.

@m8r1us
Last active March 27, 2024 14:54
Show Gist options
  • Save m8r1us/aba66a44e213e356a6b75eefbe1c7fbd to your computer and use it in GitHub Desktop.
Save m8r1us/aba66a44e213e356a6b75eefbe1c7fbd to your computer and use it in GitHub Desktop.
Azure AD group members
# Install and import the AzureAD module (if not already installed)
#Install-Module AzureAD -Force
#Import-Module AzureAD
# Connect to Azure AD
Connect-AzureAD
# Get all Azure AD groups
$AllGroups = Get-AzureADGroup -All $true
# Function to recursively get all group members
function Get-NestedGroupMembers {
param (
[string]$GroupId
)
$members = Get-AzureADGroupMember -ObjectId $GroupId
foreach ($member in $members) {
if ($member.ObjectType -eq "Group") {
# Recursively call the function for nested groups
Get-NestedGroupMembers -GroupId $member.ObjectId
} else {
# Output the non-group member details
$memberGroup = Get-AzureADUser -ObjectId $member.ObjectId
$memberGroup | Select-Object @{Name="GroupName"; Expression={$AllGroups | Where-Object {$_.ObjectId -eq $GroupId} | Select-Object -ExpandProperty DisplayName}},
DisplayName, UserPrincipalName, ObjectType
}
}
}
# Iterate through each group
foreach ($group in $AllGroups) {
Get-NestedGroupMembers -GroupId $group.ObjectId
}
# Clean up by removing the AzureAD module (optional)
# Remove-Module AzureAD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment