Solved

Get Exchange 2010 Dynamic Distribution Group Members Powershell

Posted on 2014-01-11
7
3,569 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
Make managing Office 365 email signatures a breeze

Are you using Office 365? Having trouble trying to set up email signatures for your users? Getting stressed out managing multiple signatures? Need an easier way to manage? We have a solution for you, try the most-user friendly and powerful signature management tool on the market.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…
This video discusses moving either the default database or any database to a new volume.

914 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

17 Experts available now in Live!

Get 1:1 Help Now