Solved

Get Exchange 2010 Dynamic Distribution Group Members Powershell

Posted on 2014-01-11
7
3,754 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 40

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 70

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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
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 40

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

Are You Headed to Black Hat USA 2017?

Getting ready for Black Hat next week? Kick things off with the WatchGuard Badge Challenge and test your puzzle and cipher skills. Do you have what it takes to earn our limited edition Firebox Badge? Get started today - https://crimsonthorn.net

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unified and professional email signatures help maintain a consistent company brand image to the outside world. This article shows how to create an email signature in Exchange Server 2010 using a transport rule and how to overcome native limitations …
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
To show how to generate a certificate request 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 Servers >> Certificates…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Suggested Courses

628 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