Skip to content

Get-MgmtGraphGroupTransitiveMember

MgmtGraph: Retrieves transitive group members

#Requires -Version 5.1
#Requires -Modules Microsoft.Graph.Groups

[CmdletBinding()]
Param (
    [Parameter(Mandatory = $true, Position = 0)]
    [string]$GroupId,

    [ValidateSet('AsApplication', 'AsDevice', 'AsGroup', 'AsOrgContact', 'AsServicePrincipal', 'AsUser')]
    [string]$ResultType
)

Process {
    try {
        $params = @{
            'GroupId'     = $GroupId
            'All'         = $true
            'ErrorAction' = 'Stop'
        }

        $members = $null

        if ($ResultType) {
            switch ($ResultType) {
                'AsApplication' {
                    $members = Get-MgGroupTransitiveMemberAsApplication @params
                }
                'AsDevice' {
                    $members = Get-MgGroupTransitiveMemberAsDevice @params
                }
                'AsGroup' {
                    $members = Get-MgGroupTransitiveMemberAsGroup @params
                }
                'AsOrgContact' {
                    $members = Get-MgGroupTransitiveMemberAsOrgContact @params
                }
                'AsServicePrincipal' {
                    $members = Get-MgGroupTransitiveMemberAsServicePrincipal @params
                }
                'AsUser' {
                    $members = Get-MgGroupTransitiveMemberAsUser @params
                }
            }
        }
        else {
            $members = Get-MgGroupTransitiveMember @params
        }

        $results = foreach ($m in $members) {
            $displayName = $m.AdditionalProperties['displayName']
            if ($null -eq $displayName) { $displayName = $m.DisplayName }

            $upn = $m.AdditionalProperties['userPrincipalName']
            if ($null -eq $upn) { $upn = $m.Mail }

            $type = $ResultType
            if (-not $type) { $type = $m.AdditionalProperties['@odata.type'] -replace '^#microsoft.graph.', '' }

            [PSCustomObject]@{
                GroupId           = $GroupId
                MemberId          = $m.Id
                DisplayName       = $displayName
                UserPrincipalName = $upn
                Type              = $type
                Timestamp         = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
            }
        }

        Write-Output $results
    }
    catch {
        throw
    }
}

The unique identifier of the Microsoft Graph group.

Optional. The type of transitive members to retrieve. Supported values: AsApplication, AsDevice, AsGroup, AsOrgContact, AsServicePrincipal, AsUser.

An interactive directory of PowerShell scripts.