Solved

Get Exchange 2010 Dynamic Distribution Group Members Powershell

Posted on 2014-01-11
7
3,605 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 69

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
In this video we show how to create a Distribution Group 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 Recipients >>…
This video discusses moving either the default database or any database to a new volume.

772 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