Get-ExchangeGroupMembers.psm1

function Get-ExchangeGroupMembers {
    [cmdletbinding()]
    param(
        [parameter(
            Mandatory = $true,
            Position = 0,
            ValueFromPipeline = $true)]
        [string[]]$Identity
    )
    BEGIN {
        if ((Get-ConnectionInformation).State -ne "Connected") {
            try {
                Connect-ExchangeOnline -ErrorAction Stop
            }
            catch {
                Write-Error "Could not connect to Exchange Online. Try connecting manually then run again."
                return
            }
        }
        try {
            $ADUsers = Get-ADUser -Filter * -Properties title -ErrorAction Stop -Verbose
        }
        catch {
            Write-Error "Could not get AD users, check domain connectivity."
            return
        }
    }
    PROCESS {
        foreach ($id in $Identity) {
            try {
                $group = Get-UnifiedGroup $id -ErrorAction Stop
                $members = Get-UnifiedGroupLinks $group -LinkType members | Select-Object -ExpandProperty primarysmtpaddress
            }
            catch {
                try {
                    $group = Get-DistributionGroup $id -ErrorAction Stop
                    $members = Get-DistributionGroupMember $group | Select-Object -ExpandProperty primarysmtpaddress
                }
                catch {
                    Write-Error "Could not find group information for identity $id"
                }
            }
            foreach ($user in $members) {
                $ADUsers | Where-Object UserPrincipalName -eq $user | ForEach-Object {
                    [PSCustomObject]@{
                        GroupType = $group.RecipientTypeDetails
                        DisplayName = $group.DisplayName
                        PrimarySmtpAddress = $group.PrimarySmtpAddress
                        Name = $_.name
                        UserPrincipalName = $_.UserPrincipalName
                        SamAccountName = $_.SamAccountName
                        Title = $_.title
                    }
                }
            }
        }
    }
    END {
        <#
        #>

    }
}