I had a need to get distribution group members for a specific group. Most people seem to install the free “ActiveRoles Management Shell for Active Directory” from Quest Software when they are facing a task such as this.
With the Quest “ActiveRoles Management Shell”, you can do this like “Get-QadGroupMember <group name>–Indirect”
I’m kind of contrary, and so I wanted to do the same thing with the Exchange Management Shell that came with our Exchange 2007 installation. So, I created the script shown here. Save it as get-exgrouprecurse.ps1.
Run it as:
get-exgrouprecurse -group <group name>
or get-exgrouprecurse <group name>
- #####################################
- # #
- # Script to retreive group members #
- # #
- # Karl Mitschke March 7 2008 #
- # #
- #####################################
- ######################################
- # heavily modified from recipe 7.3 #
- # in 'Active Directory Cookbook' #
- # by Robbie Allen #
- ######################################
- #requires -pssnapin Microsoft.Exchange.Management.PowerShell.Admin
- param($group)
- $UnknownGroup = @{}
- function DisplayMembers($group)
- {
- $SubGroup = @{}
- $AllMembers = @()
- if(!$group)
- {
- $group = Read-Host "Enter the groups display name"
- }
- if ($group.Contains("'"))
- {
- $group = $group.Replace("'",'"')
- }
- if ($group -eq "/?")
- {
- Write-Host "Usage:"
- Write-Host ""
- Write-Host "get-exgrouprecurse -group <group name>"
- Write-Host ""
- Write-Host "or get-exgrouprecurse <group name>"
- Write-Host "Returns an object containing the group member, and the group name."
- break
- }
- $validate = Get-Group $group
- if ($validate.RecipientTypeDetails.ToString() -match "mail")
- {
- $searchGroup = Get-DistributionGroupMember $group
- if ($searchGroup)
- {
- foreach ($member in $searchGroup)
- {
- $membertype = $member.RecipientTypeDetails
- if($membertype -match "Group")
- {
- $samname = $member.SamAccountName.ToString()
- if ($SubGroup.ContainsKey($samname) -eq $true)
- {
- Write-Output "^ already seen group member (stopping to avoid loop)"
- }
- else
- {
- $SubGroup.Add($samname,$member.DisplayName.ToString())
- }
- }
- else
- {
- if($member.PrimarySmtpAddress -and $member.RecipientTypeDetails -notcontains "group")
- {
- $obj = new-object psObject
- $obj | Add-Member -membertype noteproperty -Name GroupMember -Value $member.name
- $obj | Add-Member -MemberType noteproperty -Name GroupName -Value $group
- $AllMembers += $obj
- }
- }
- }
- }
- else
- {
- $UnknownGroup.add($group,1)
- }
- if($SubGroup.Values.Count -gt 0)
- {
- foreach ($subGroup in $SubGroup.values)
- {
- DisplayMembers $subGroup
- }
- }
- if ($UnknownGroup.Keys.Count -gt 0)
- {
- foreach ($LostGroup in $UnknownGroup.keys)
- {
- $obj = new-object psObject
- $obj | Add-Member -membertype noteproperty -name GroupMember -Value "Cannot enumerate group"
- $obj | Add-Member -MemberType noteproperty -Name GroupName -Value $LostGroup
- $AllMembers += $obj
- }
- $UnknownGroup.Clear()
- }
- }
- else
- {
- Write-Output "$group does not appear to be mail enabled."
- }
- Write-Output $AllMembers
- }
- DisplayMembers $group
#1 by Lars on August 4, 2010 - 13:59
Hi! Excellent script thanks!!
Question, how do I make the script pull out the email address and displayname of the users in the group
is that possible?
thanks Lars
#2 by karlmitschke on August 4, 2010 - 17:32
Lars;
I’m a little confused – the $member.name IS the dis[play name for me.
Regardless, adding the following to the two sections where we build the $obj will work:
$obj | Add-Member -MemberType noteproperty -Name DisplayName -Value $member.DisplayName
$obj | Add-Member -MemberType noteproperty -Name EmailAddress -Value $member.PrimarySMTPAddress
Karl
#3 by david on September 28, 2010 - 17:38
Thanks for the script. While I was trying to accomplish something slightly different, your code helped!
#4 by karlmitschke on September 28, 2010 - 23:42
Glad to help.
#5 by albertwt on February 24, 2011 - 17:27
yes Karl is great 🙂
#6 by Raj on February 28, 2011 - 11:33
Hey Karl,
I want to attach the photos of the users on Outlook 2007. I have named the photos to match with the display names of the users. Also I have created a .CSV file with all the users from the active directory. The photos are located in a folder on the same shared drive. I know that I need to use Get-DistributionGroupMember with foreach loop. But I really dont know how to go about it. Please can you help me with this.
Thanks in advance.
#7 by Karl Mitschke on August 29, 2011 - 09:25
Raj;
That’s outside ths scope of this article.
You could email me and I will look into this.
Karl
#8 by vikram khanna on October 25, 2011 - 05:41
Hi Karl – thanks a lot for this script – how can I export the output to an excel file. Please let me know.
Thanks.
Vikram
#9 by Karl Mitschke on October 25, 2011 - 08:17
get-exgrouprecurse.ps1 -Group <groupname. | Export-Csv -Path C:\scripts\.csv -NoTypeInformation
#10 by keltz on December 14, 2011 - 02:35
hi karl,
how do i fine tune your script to export all the members of all the distribution group in to a csv file for easy reading.
#11 by Karl Mitschke on March 20, 2012 - 10:06
The following should work:
.\get-exgrouprecurse.ps1 -Group <groupname. | Export-Csv -Path C:\scripts\.csv -NoTypeInformation
#12 by thomesp on November 28, 2012 - 08:24
script works great. Thanks! One Question, can we show the displayname for memebers of groups?
#13 by Karl Mitschke on November 28, 2012 - 09:43
You should be able to modify line 67 as
$obj | Add-Member -membertype noteproperty -Name GroupMember -Value $member.DisplayName
#14 by thomesp on November 28, 2012 - 10:18
Thanks very much!
#15 by tim on July 30, 2013 - 15:13
I am using Exchange 2010 without any 3rd party addins (like Quest). I can’t seem to find how to get this command in 2010:
get-exgrouprecurse
is the name of your script ‘get-exgrouprecurse’
#16 by Karl Mitschke on July 31, 2013 - 08:41
Yes, that’s the script name.
Karl