Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get Exchange 2010 Dynamic Distribution Group Members Powershell

Posted on 2014-01-11
7
Medium Priority
?
3,829 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 41

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 71

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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
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 41

Accepted Solution

by:
footech earned 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Here in this article, you will get a step by step guidance on how to restore an Exchange database to a recovery database. Get a brief on Recovery Database and how it can be used to restore Exchange database in this section!
If you have come across a situation where you need to find some EDB mailbox recovery techniques, then here you will find the same. In this article, we will take you through three techniques using which you will be able to perform EDB recovery. You …
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…
Loops Section Overview

782 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