25,081 questions
try the following:
Install-Module -Name Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "Group.Read.All", "User.Read.All"
# Define the output file
$outputFile = "AzureADGroupsMembers.csv"
# Get all security groups
$groups = Get-MgGroup -Filter "securityEnabled eq true and mailEnabled eq false" -All
# Initialize an array to hold the results
$groupMembers = @()
# Loop through each group and get its members
foreach ($group in $groups) {
$members = Get-MgGroupMember -GroupId $group.Id -All
foreach ($member in $members) {
$groupMembers += [PSCustomObject]@{
GroupName = $group.DisplayName
GroupId = $group.Id
MemberName = $member.DisplayName
MemberType = $member.ODataType
MemberId = $member.Id
}
}
}
# Export the results to a CSV file
$groupMembers | Export-Csv -Path $outputFile -NoTypeInformation
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin