Solved

Get Exchange 2010 Dynamic Distribution Group Members Powershell

Posted on 2014-01-11
7
3,547 Views
Last Modified: 2014-07-02
I'm presently using the below script which exports each of my dynamic distribution group into a csv file for each list.  I would like to modify the script, so instead of creating individual csv files for each dynamic distribution group, it populates the results into a single csv file that includes all the existing columns, but adding the name of the dynamic distribution group. I'm a powershell novice, so I would need the exact command provided I can just replace with my current one. Thanks in advance!

Write-Host "Getting all Dynamic Distribution Groups"
$DDLs = Get-DynamicDistributionGroup -ResultSize Unlimited
$primaryCounter = 0
Foreach ($DDL in $DDLs)
{
	$primaryCounter++
	Write-Progress -Activity "Processing Groups" -Status "Current Group: $($DDL.Name) ($($primaryCounter)/$($DDLs.Count))" -Id 1 -PercentComplete (($primaryCounter/$DDLs.Count)*100)
	$log = $DDL.Name + ".csv"
	"Alias	Company	BusinessUnit	Location	Department	DisplayName	PrimarySmtpAddress	RecipientType	SamAccountName	Title" | Out-File $log -Append
	Write-Host "Finding all users that are currently members of $($DDL.Name)"
	$members = Get-Recipient -RecipientPreviewFilter $DDL.RecipientFilter -ResultSize Unlimited
	$subCounter = 0
	foreach ($member in $members)
	{
		$subCounter++
		Write-Progress -Activity "Processing Group Members" -Status "$($member.alias) ($($subCounter)/$($members.count))" -Id 2 -ParentId 1 -PercentComplete (($subCounter/$members.count)*100)
		$entry = $member.alias + "	" + $member.company + "	" + $member.customAttribute1 + "	" + $member.customAttribute2 + "	" + $member.department + "	" + $member.DisplayName + "	" + $member.PrimarySmtpAddress + "	" + $member.recipientType + "	" + $member.SamAccountName + "	" + $member.title
		$entry | Out-File $log -Append
	}
}

Open in new window

0
Comment
Question by:fireguy1125
7 Comments
 
LVL 39

Expert Comment

by:footech
ID: 39773752
Well, it's not really creating what I would call a .CSV, just a formatted text file.  As such I'm not sure how you want the output to look like.  If you were outputting an actual .CSV then the way to do it would be to add the distribution list name as another column.

Here's one sample of what you could do.
Write-Host "Getting all Dynamic Distribution Groups"
$DDLs = Get-DynamicDistributionGroup -ResultSize Unlimited
$primaryCounter = 0
$log = "DDL.csv"
"Alias	Company	BusinessUnit	Location	Department	DisplayName	PrimarySmtpAddress	RecipientType	SamAccountName	Title" | Out-File $log -Append
Foreach ($DDL in $DDLs)
{
	$primaryCounter++
	Write-Progress -Activity "Processing Groups" -Status "Current Group: $($DDL.Name) ($($primaryCounter)/$($DDLs.Count))" -Id 1 -PercentComplete (($primaryCounter/$DDLs.Count)*100)
	"" | Out-File $log -Append
	$DDL.name | Out-File $log -Append
	Write-Host "Finding all users that are currently members of $($DDL.Name)"
	$members = Get-Recipient -RecipientPreviewFilter $DDL.RecipientFilter -ResultSize Unlimited
	$subCounter = 0
	foreach ($member in $members)
	{
		$subCounter++
		Write-Progress -Activity "Processing Group Members" -Status "$($member.alias) ($($subCounter)/$($members.count))" -Id 2 -ParentId 1 -PercentComplete (($subCounter/$members.count)*100)
		$entry = $member.alias + "	" + $member.company + "	" + $member.customAttribute1 + "	" + $member.customAttribute2 + "	" + $member.department + "	" + $member.DisplayName + "	" + $member.PrimarySmtpAddress + "	" + $member.recipientType + "	" + $member.SamAccountName + "	" + $member.title
		$entry | Out-File $log -Append
	}
}

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39774959
Is it really your intention to create a manully formatted tabular text file? Such is only suited for displaying on console or in an editor like Notepad. Even using as email body will destroy the layout. What's the result file used for?
0
 
LVL 1

Author Comment

by:fireguy1125
ID: 39777633
No it doens't have to be manually formatted, just in csv format.  What I am going to essentially be doing is importing into Excel for compare between active employee list.  Not really sure how the code works and it does seems involved for what I'm looking to do, so if it can be simplified all the better.  This is especially true, because the script only works when I have pre-existing csv file names of the dynamic distribution groups located in a folder and run the script from within that folder.
0
Don't lose your head updating email signatures!

Do your end users still have the wrong email signature? Do email signature updates bore you or fill you with a sense of dread? You can make this a whole lot easier on yourself by trusting an Exclaimer email signature management solution. Over 50 million users do...so should you!

 
LVL 1

Author Comment

by:fireguy1125
ID: 39777659
footech, your script works, however I need it so the dynamic distribution group is added as an additional column, and the value is displayed with each resulting mailbox.
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 39777936
Try something like this.
$DDLs = Get-DynamicDistributionGroup -ResultSize Unlimited
$primaryCounter = 0
$log = "DDL.csv"
@(Foreach ($DDL in $DDLs)
{
	$primaryCounter++
	Write-Progress -Activity "Processing Groups" -Status "Current Group: $($DDL.Name) ($($primaryCounter)/$($DDLs.Count))" -Id 1 -PercentComplete (($primaryCounter/$DDLs.Count)*100)
	Write-Host "Finding all users that are currently members of $($DDL.Name)"
	$members = Get-Recipient -RecipientPreviewFilter $DDL.RecipientFilter -ResultSize Unlimited
	$subCounter = 0
	foreach ($member in $members)
	{
		$subCounter++
		Write-Progress -Activity "Processing Group Members" -Status "$($member.alias) ($($subCounter)/$($members.count))" -Id 2 -ParentId 1 -PercentComplete (($subCounter/$members.count)*100)
		$member | Select @{n="DistributionList";e={$DDL.name}},Alias,Company,@{n="BusinessUnit";e={$_.customAttribute1}},@{n="Location";e={$_.customAttribute2}},Department,DisplayName,PrimarySmtpAddress,RecipientType,SamAccountName,Title
	}
}) | Export-CSV $log -notype

Open in new window

0
 
LVL 1

Author Closing Comment

by:fireguy1125
ID: 39820913
thanks so much, perfect!
0
 

Expert Comment

by:waypointsupport
ID: 40172594
I am having trouble with the original script running on Exchange 2010. For some reason it returns the entire list of exchange accounts for each dynamic distribution group instead of just listing the members of the group.
0

Featured Post

Want to promote your upcoming event?

Are you going to an event? Are you going to be exhibiting at a tradeshow? Talking at a conference? Using a promotional banner in your email signature ensures that your organization’s most important contacts stay in the know and can potentially spread the word about the event.

Join & Write a Comment

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …
In this video we show how to create an Address List in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Organization >> Ad…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now