How to List of Azure AD groups for a Service Principal using Power shell




I want to fetch list of Azure AD groups which are assigned/ added as member for a service principal.


I am using below script but it is taking too much of time due to for loop each AD group check, Can you please suggest any other way we can do in optimized.


$ADGroups = Get-AzADGroup
foreach ($ADGroup in $ADGroups)
$GroupMembers = Get-AzADGroupMember -ObjectId $ADGroup.Id | where-Object {$_.Type -eq "ServicePrincipal"}

$GroupMember = $GroupMembers | where-Object {$_.DisplayName -eq "xxxxxxxxx"}
$string = [pscustomobject]@{
    "ServicePrincipalName" = $GroupMember.DisplayName
    "ADGroupName" = $ADGroup.DisplayName


1 Reply
best response confirmed by Brahmaiah (Contributor)