Forum Discussion

Brahmaiah's avatar
Brahmaiah
Brass Contributor
Aug 27, 2020

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

Hi,

 

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"}
if($GroupMember)
{
$string = [pscustomobject]@{
    "ServicePrincipalName" = $GroupMember.DisplayName
    "ADGroupName" = $ADGroup.DisplayName
    }
$string

}

}

Resources