Hello @Kevin Alive , thank you for reaching out. Please do check if the following script helps meet your requirements:
Connect-AzureAD
$PathCsv = "C:\temp\GroupMembers.csv"
$GroupName = Read-Host -Prompt "Enter group name to search"
$groups = Get-AzureADGroup -SearchString $GroupName
$groupCount = $groups | measure
$count = $groupCount.Count
$groupMembers = foreach($group in $groups){
$GroupId = $group.ObjectId
$GroupName = $group.DisplayName
Write-Progress -Activity "No of Groups found: $count`
Fetching members for GroupName: $GroupName"
Start-Sleep -Milliseconds 200
Get-AzureADGroupMember -ObjectId $GroupId | Select-Object -Property @{Name = 'GroupName'; Expression= {$GroupName}}, DisplayName, UserPrincipalName
}
$groupMembers | Export-Csv -Path $PathCsv -NoTypeInformation -Append
Hope this helps.
Do let us know if this helps and if there are any more queries around this, please do let us know so that we can help you further. Also, please do not forget to accept the response as an Answer; if the above response helped in answering your query.