Get Distribution list members recursively including contact objects
Get Distribution list members recursively including contact objects

I have written this script to get recursively the members of the distribution lists in a specific OU. It is simple if your Active Directory distribution lists contain user account.
You will have problem if your DL includes contact objects :

Technet article https://technet.microsoft.com/en-us/library/ee617193.aspx:
The Get-ADGroupMember cmdlet gets the members of an Active Directory group. Members can be users, groups, and computers.

The solution, in that case, is to use the cmdlet get-adobject. The script below extract recursively the members of the dl in a specific OU and then export the result in text files ; one text file per dl that contains members dn.

And now the script:

function Get-AllADGroupMember ($ADServer,$GroupName) {
	$member_array = @()
	Get-ADObject -Identity $GroupName -Properties member -Server $ADServer":3268" | % { $_.member | % {
		$member1 = $_
		$member_array += $member1
		if ((Get-ADObject -Identity $member1 -Server $ADServer":3268").ObjectClass -eq "group" ){
			Get-AllADGroupMember $ADServer $member1
		}
	}}
	return $member_array
}

$ADServer = "domain.net"

$grouplist = get-adgroup -SearchBase "OU=Distribution Lists,DC=domain,DC=net" -filter * -Properties mail

$grouplist | % {
	$groupname = $_.DistinguishedName
	$groupmail = $_.mail

	$arr = Get-AllADGroupMember $ADServer $groupname

	$arr | out-file -FilePath "c:\temp\$groupmail.txt" -Encoding unicode
}

References

Get-ADObject

<>

My Powershell script categories

Get Distribution list members recursively including contact objects

Leave a Reply

Your email address will not be published. Required fields are marked *