Extract all mail enabled groups with PowerShell and ADSI

I’ve just finished off this first attempt at an actual useful PowerShell script. This should extract all Mail Enabled groups – that’s both security and distribution groups – and present them in a human readable format. Though I’ve played with PowerShell a little, I’ve not had need to use it for anything useful until now.

Unfortunately, if a group is a member of another group, it won’t expand that group but since the data is exported in human readable, you can just look at the member list of that group.

$RootDN = [ADSI] ''
$Searcher = New-Object System.DirectoryServices.DirectorySearcher($RootDN)

$Searcher.Filter = "(&(objectClass=group)(mail=*))"

$MeGroups = $searcher.FindAll()

Write-Host "There are" $MeGroups.Count "mail enabled groups (Security & Distribution)"

ForEach ($Group in $MeGroups) {

 $GroupDN = [ADSI]$Group.Path
 Write-Host $GroupDN.displayName "("$GroupDN.mail")"

 ForEach ($Member in $GroupDN.member) {

 $Member = $Member.ToString()
 $Entry = $Member.Split(",")
 $Entry = $Entry[0] -replace("CN=","")

 Write-Host `t $Entry

 }

 Write-Host `r`n

}

-Lewis

This entry was posted in Scripting. Bookmark the permalink.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>