Friday, August 31, 2007

Exchange 2007 powershell - enumerate all members of all distribution groups

Updated 4/22/08 - Fixed missing characters lost in previous upload

So I had a request to generate a report showing all Email Distribution Groups and the members of each group. I'm currently learning PowerShell so it worked out good for practice. My first approach was to just build a generic one for any active directory group and OU, etc but after a few roadblocks I wound up at the right way to build it using the build in exchange 2007 cmdlets.

# Enumerates all members of all Distribution Lists in Exchange 2007. Uses cmdlets from exch2007
# Updated 4/22/08
# By: Gnawgnu

#first get all distributionlists
$dl = get-distributiongroup

#prepare and output file
$currDate = get-date
write-host "Email groups as of: " $currDate | out-file 'c:\temp\emailgroupmembers.txt'


#then enumerate through them all and get all group members.
foreach ($group in $dl) {

$groupName = "Group Name: " + $group.name
write-host $groupName -foregroundcolor Green
write-host "Owner: " $group.ManagedBy.Name -foregroundcolor Green
$groupName | out-file -append 'c:\temp\emailgroupmembers.txt'
$group.ManagedBy.Name | out-file -append 'c:\temp\emailgroupmembers.txt'
$dlgm = get-distributionGroupMember $group.name.ToString()
$dlgm | fw | out-file -append 'c:\temp\emailgroupmembers.txt'

}

Yeah, I know some parts could be optimized more but this script works. Have fun with it.

18 comments:

Albert Widjaja said...

Hey man,

I caught an error ?

Unexpected token 'write-host' in expression or statement.
At C:\Get-DG.ps1:15 char:53
+ $groupName = "Group Name: " + $group.name write-host <<<< $groupName -foregr
oundcolor Green

Gnawgnu said...

Good Catch! It appears a pipe and some other stuff got eaten. I've posted an updated version now. And I copied and pasted it from the page back to the exch server and ran it for good measure.

Anonymous said...

this line doesn't seem to work, is it supposed to return the dl owner?

Anonymous said...

Just what I needed. Thanks.

DeathToSpam said...

This was immensely helpful -- thank you!

Anonymous said...

This works great but I was wondering if there was a way to export this into a single csv?

Gnawgnu said...

You should be able to use the export-csv cmdlet in powershell to redirect output to a file instead. You should be able to buffer all the outputs in this script to another variable and then pipe it into the export-csv command.

http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/export-csv.mspx

tcnolan said...

I wrote a similar script, but the one I wrote recursively drills down into sub-groups and includes dynamic distribution groups as well in the list. Some of you might find this helpful: http://www.tinyint.com/index.php/2009/05/24/enumerate-distribution-group-members/

Gnawgnu said...

Cool, I'm generally bad with recursion myself. Thanks for the contribution.

Duece Fuego said...

Good job! Worked great for me. Thanks.

Anonymous said...

Hey there....quick question please. I am stepping through your "enumerate members of dl" code line by line and have an issue. When I write-host and pipe it to out-file, it just displays it to the screen. The out file is created but is blank. I did a test and simply put write-host "hello" | out-file c:\hello.txt Same thing, writes hello to screen and the txt file is created but blank. Any ideas? Thanks (dborsh@wowway.com)

Gnawgnu said...

I ran into problems like that when I went to exchange 2010 because of some changes from 2007 and powershell. (http://gnawgnu.blogspot.com/2010/05/exchange-2010-email-list-of-all.html). Some fields like owner are containers now that display fine when using a write-host but for some reason have to be manually extracted out for output to csv. I don't have any 2k7 servers anymore so I can't really test it. Good luck.

Anonymous said...

I am trying to match the photos of the members located in different folder to these names from the .CSV file. Trying to use Get-DistributionGroupMember. I have named the photos as "LastName, FirstName" as they appear on outlook. how should I go about this?

Gnawgnu said...

Well, actually if all your photos names match the display name there's a bulk import script over at exchangeinbox.com for mass importing. All your files will need to be jpgs. Depending on how your AD accounts are set, you may need to remove the comma in your filenames before you start. (Make a backup of your photos first in case I'm wrong.)
http://exchangeinbox.com/article.aspx?i=156
If you want to test it first, put a # in front of his Import line and use Get-User $User to make sure everything matches up first.
And that Import line of his wraps around - both lines are actually 1 line of code.

Gnawgnu said...

BTW, for the code in the link above, it's looking to match up the 'Display Name' attribute in AD to your filename preceding the .jpg

sadissa said...

Great thanks, Gnawgnu!

This script works perfect and the output is light.

krish said...

Is it possible to get users who all the member of a dynamic distribution group? I want to get all the members of Dynamic distribution group from Domain A and create a contact for them on Domain B?
Thanks
Krishna

Gnawgnu said...

I'm not running 2007 anywhere anymore but there is a function in there for pulling out all the members of a dynamic distribution group. There's powershell code at:
http://technet.microsoft.com/en-us/library/bb232019%28EXCHG.80%29.aspx