# encoding: ascii
# api: powershell
# title: Get-ADGroupMember -R
# description: This is how I would go about recursively getting AD Group Members.
# version: 0.1
# type: function
# author: ERROR_SUCCESS
# license: CC0
# function: Get-MembersFromAD
# x-poshcode-id: 4181
# x-archived: 2017-05-16T03:57:50
# x-published: 2014-05-24T03:10:00
#
# http://www.powershellmagazine.com/2013/05/23/pstip-retrieve-group-membership-of-an-active-directory-group-recursively/
# It’s longer than Shay’s .NET way. It looks like it might return duplicates too. I don’t know dotNET. I do like the type accelerator.
#
function Get-MembersFromAD{
    [cmdletbinding()]
    Param(
        [Parameter(Mandatory=$true)]
        [string]$DistinguishedGroupName
    )

    Write-Verbose "Getting `"$DistinguishedGroupName`""
    $group = [adsi]"LDAP://$DistinguishedGroupName"

    Write-Verbose "Getting members ..."
    foreach($DN in $group.member){        
        $member = [adsi]"LDAP://$DN"
        if($member.objectClass -contains 'group'){
            Write-Verbose "RECURSIVE"
            $peeps += @(Get-MembersFromAD $member.distinguishedName -Verbose)
        }
        else{
            $peeps += @($member.sAMAccountName)
        }
    }

    return $peeps
}

Get-MembersFromAD 'CN=Group Name,OU=Groups,OU=Practice,OU=Location,DC=company,DC=com' -Verbose
