Powershell command to list all distribution groups and members of those groups.
I have been tasked with listing all of the distribution groups in our org and showing all members of every group.
I have tired using get-distributiongroup | get-distributiongroupmember
But it only show the members and not the groups. Any scripters out there got a quick one liner I could use? I really don't want to have to go through all 200 distribution groups manually exporting members to a .csv for each group.
Thanks in advance!
November 3rd, 2009 12:59am
If you use Quest Free PowerShell Commands for Active Directory you can do a:Get-QadGroup -GroupType "Distribution"| Get-QadGroupMember -Indirecthttp://www.quest.com/powershell/activeroles-server.aspxIn EMS it's harder, but I can post a script if you'd like,Karl
Free Windows Admin Tool Kit Click here and download it now
November 3rd, 2009 2:09am
I have seen the Quest Free PowerShell Commands but we are not using them. If you wouldn't mind I would really appreciate it if you could post your script. I would like to see how this works natively in EMS.
Thank you
November 3rd, 2009 4:49am
Hi,Does this help....$DGrps = Get-DistributionGroupForEach ($DGrp in $DGrps) {Write-Host DGrp.name -Filepath <yourfilename> -append $GrpMbrs = Get-DistributionGroupMember -id $DGrpforeach ($GrpMbr in $GrpMbrs) {Write-Host $GrpMbr.DistinguishedName -Filepath <yourfilename> -append }}Nitin Gupta (gupnit) | MVP - Exchange | http://www.nitingupta.in/blogs
Free Windows Admin Tool Kit Click here and download it now
November 3rd, 2009 5:23am
This does appear to work but how do I go about outputting the file to somewhere? I entered C:\data.csv for <yourfilename> but never see the file created and when I create a blankfile called C:\data.csv hoping that the data will append to the file it remains empty.Thank for your help!
November 3rd, 2009 6:12pm
This is my script:usage : get-exgrouprecurse <groupname> will display group members and the subgroup on screen$group = get-exgrouprecurse <groupname> $group |Export-Csv <file name> -NoTypeInformation will export the group members to a file
#####################################
# #
# Script to retreive group members #
# #
# Karl Mitschke March 7 2008 #
# #
#####################################
######################################
# heavily modified from recipe 7.3 #
# in 'Active Directory Cookbook' #
# by Robbie Allen #
######################################
#requires -pssnapin Microsoft.Exchange.Management.PowerShell.Admin
param($group)
$UnknownGroup = @{}
function DisplayMembers($group)
{
$SubGroup = @{}
$AllMembers = @()
if(!$group)
{
$group = Read-Host "Enter the groups display name"
}
if ($group.Contains("'"))
{
$group = $group.Replace("'",'"')
}
if ($group -eq "/?")
{
Write-Host "Usage:"
Write-Host ""
Write-Host "get-exgrouprecurse -group <group name>"
Write-Host ""
Write-Host "or get-exgrouprecurse <group name>"
Write-Host "Returns an object containing the group member, and the group name."
break
}
$validate = Get-Group $group
if ($validate.RecipientTypeDetails.ToString() -match "mail")
{
$searchGroup = Get-DistributionGroupMember $group
if ($searchGroup)
{
foreach ($member in $searchGroup)
{
$membertype = $member.RecipientTypeDetails
if($membertype -match "Group")
{
$samname = $member.SamAccountName.ToString()
if ($SubGroup.ContainsKey($samname) -eq $true)
{
Write-Output "^ already seen group member (stopping to avoid loop)"
}
else
{
$SubGroup.Add($samname,$member.DisplayName.ToString())
}
}
else
{
if($member.PrimarySmtpAddress -and $member.RecipientTypeDetails -notcontains "group")
{
$obj = new-object psObject
$obj | Add-Member -membertype noteproperty -name GroupMember -Value $member
$obj | Add-Member -MemberType noteproperty -Name GroupName -Value $group
$AllMembers += $obj
}
}
}
}
else
{
$UnknownGroup.add($group,1)
}
if($SubGroup.Values.Count -gt 0)
{
foreach ($subGroup in $SubGroup.values)
{
DisplayMembers $subGroup
}
}
if ($UnknownGroup.Keys.Count -gt 0)
{
foreach ($LostGroup in $UnknownGroup.keys)
{
$obj = new-object psObject
$obj | Add-Member -membertype noteproperty -name GroupMember -Value "Cannot enumerate group"
$obj | Add-Member -MemberType noteproperty -Name GroupName -Value $LostGroup
$AllMembers += $obj
}
$UnknownGroup.Clear()
}
}
else
{
Write-Output "$group does not appear to be mail enabled."
}
Write-Output $AllMembers
}
DisplayMembers $group
Free Windows Admin Tool Kit Click here and download it now
November 3rd, 2009 6:24pm
Thanks for posting your script Karl. In less I am doing something wrong the script still asks that I submit the anme of the group I want to know about. I need to know this information for all DL Groups at one time. Is there away to tell the script to give me information about all of my groups?Thank you
November 3rd, 2009 9:36pm
Try this:Get-DistributionGroup -ResultSize unlimited |%{.\get-exgrouprecurse $_.Name}I am pretty sure this will not show the name of the main group - like:Group Ahas a member member 1and a member Sub Group 1Which has a memberMember 2I could modify it at some point, or perhaps you can follow it and modify it.Karl
Free Windows Admin Tool Kit Click here and download it now
November 3rd, 2009 11:56pm
At a quick glance, Nitin's script should do what you're looking for with a few modifications. Write-host is what's preventing the output from going to the file like you're trying to accomplish. You can modify the script to use the re-directors, or just use out-file. I haven't tested it, but this should work (just fixing a few bits from Nitin's script, but other then that, really just a copy and paste of his post.) $DGrps = Get-DistributionGroup ForEach ($DGrp in $DGrps) {out-file -inputobject $DGrp.name -Filepath <yourfilename> -append $GrpMbrs = Get-DistributionGroupMember -id $DGrp foreach ($GrpMbr in $GrpMbrs) {out-file -inputobject $GrpMbr.DistinguishedName -Filepath <yourfilename> -append }} Good luck, Dan Holton
November 4th, 2009 3:25am
Thanks Mate....I have not tested it, I should have mentioned that, thought it should do the trick.Nitin Gupta (gupnit) | MVP - Exchange | http://www.nitingupta.in/blogs
Free Windows Admin Tool Kit Click here and download it now
November 4th, 2009 5:56am
This works on my SBS 2008 box. Nice if anybody out there could test the script as well. There are some quirks with PowerShell v1 (and my thinking). You can easily add a Write-Host $_ before the For loop and Write-Host $member inside the For loop to get screen output or just see what is going on.
# Initialize array with two fields:
# Distribution group, Members
$totalObj = @()
# Retrieve all DGs
$temp = Get-DistributionGroup -ResultSize Unlimited |
# Loop through all distribution groups
ForEach-Object {
# Add the members of the DG to an array
[array]$mem = Get-DistributionGroupMember -id $_
# Loop through the DG and assign each member name to the variable $member
for ($i = 0; $i -lt $mem.Count; $i++) {
$member = $mem[$i].name
# Create instance of object of type .NET
$obj = New-Object System.Object
# Add the name of the DG to the object
$obj | Add-Member -MemberType NoteProperty -Value $_.Name -Name 'Distribution Group' -Force
# Add the member name to the object
$obj | Add-Member -MemberType NoteProperty -Value $member -Name 'Members' -Force -PassThru
# Add the object to the array
$totalObj += $obj
}
}
# Pipe output to .csv file
$totalObj | Export-Csv -Encoding 'Unicode' c:\temp\ngtest.csv
The output is written like this to the csv. file:
"Distribution Group",Members
"All Users","Jon-Alfred Smith"
"All Users","Julie Smith"
"Windows SBS Administrators","Standard User with administration links"
"Windows SBS Administrators","Jon-Alfred Smith"
If you just want to have the name of the DG once, change this line: Only add the name the first time, when the counter is zero:
# Add the name of the DG to the object
if ($i -eq 0) {
$obj | Add-Member -MemberType NoteProperty -Value $_.Name -Name 'Distribution Group' -Force
}
MCTS: Messaging | MCSE: S+M | Small Business Specialist
November 4th, 2009 6:09pm
Jon-alfred:I was looking for a way on how to do this, and your script was exactly what i was looking for. Thanks!
Free Windows Admin Tool Kit Click here and download it now
November 17th, 2009 10:54pm
Can someone help provide some information on how to modify this script?
I would like the output to show what groups the users belong to. So for Joe Smith, I want to see that he is part of Accounting, NY Office, FAS etc.
May 6th, 2010 11:32pm
Can someone help provide some information on how to modify this script?
I would like the output to show what groups the users belong to. So for Joe Smith, I want to see that he is part of Accounting, NY Office, FAS etc.
Free Windows Admin Tool Kit Click here and download it now
May 6th, 2010 11:32pm
Can someone help provide some information on how to modify this script?
I would like the output to show what groups the users belong to. So for Joe Smith, I want to see that he is part of Accounting, NY Office, FAS etc.
This will work
Get-DistributionGroup | where { (Get-DistributionGroupMember $_ | foreach {$_.PrimarySmtpAddress}) -contains "user@domain.com"
Put primary smtp address of the mailbox user and it will show all the DLs that user is member of.
A discussion on it is already in progress here:
http://social.technet.microsoft.com/Forums/en-US/exchangesvradmin/thread/6db0cebc-dc14-4c54-ab6c-2cd23b52d5b5
Although it is one liner off course u can modify it with the scripts posted above by Jon and karl.
Regards,
Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM)
www.HostingController.com
May 7th, 2010 12:08am
Hi,
Does this help....
$DGrps = Get-DistributionGroup
ForEach ($DGrp in $DGrps) {Write-Host DGrp.name -Filepath <yourfilename> -append $GrpMbrs = Get-DistributionGroupMember -id $DGrp
foreach ($GrpMbr in $GrpMbrs) {Write-Host $GrpMbr.DistinguishedName -Filepath <yourfilename> -append }}
Nitin Gupta (gupnit) | MVP - Exchange | http://www.nitingupta.in/blogs
Hi Nitin Gupta,
thanks for the script.
Executing just the code:
$DGrps = Get-DistributionGroup
ForEach ($DGrp in $DGrps) {Write-Host DGrp.name -Filepath <yourfilename> -append $GrpMbrs = Get-DistributionGroupMember -id $DGrp
foreach ($GrpMbr in $GrpMbrs) {Write-Host $GrpMbr.DistinguishedName -Filepath <yourfilename> -append }}
I get an output like this:
DGrp.name -Filepath c:\Temp\1.txt -append = Get-DistributionGroupMember -id Group1
-Filepath c:\Temp\1.txt -append
DGrp.name -Filepath c:\Temp\1.txt -append = Get-DistributionGroupMember -id Group2
-Filepath c:\Temp\1.txt -append
DGrp.name -Filepath c:\Temp\1.txt -append = Get-DistributionGroupMember -id Group3
-Filepath c:\Temp\1.txt -append
DGrp.name -Filepath c:\Temp\1.txt -append = Get-DistributionGroupMember -id Group4
-Filepath c:\Temp\1.txt -append
DGrp.name -Filepath c:\Temp\1.txt -append = Get-DistributionGroupMember -id Group5
-Filepath c:\Temp\1.txt -append
DGrp.name -Filepath c:\Temp\1.txt -append = Get-DistributionGroupMember -id Group6
-Filepath c:\Temp\1.txt -append
What is wrong? Thanks a lot!Piero Bacarella - IT Professional - Rome(Italy) - http://www.it-resources.info
Free Windows Admin Tool Kit Click here and download it now
January 7th, 2011 10:34am
On Fri, 7 Jan 2011 15:30:00 +0000, Piero Bacarella wrote:
>Hi, Does this help.... $DGrps = Get-DistributionGroup ForEach ($DGrp in $DGrps) {Write-Host DGrp.name -Filepath <yourfilename> -append $GrpMbrs = Get-DistributionGroupMember -id $DGrp foreach ($GrpMbr in $GrpMbrs) {Write-Host $GrpMbr.DistinguishedName
-Filepath <yourfilename> -append }}
Don't you just love the way HTML munges things? :-(
Besides the formatting, there was a missing "$" in the 1st "ForEach".
I added semicolons at the end of each statement so they don't all run
together if you just copy the text.
$DGrps = Get-DistributionGroup;
ForEach ($DGrp in $DGrps) {
Write-Host $DGrp.name -Filepath <yourfilename> -append;
$GrpMbrs = Get-DistributionGroupMember -id $DGrp;
foreach ($GrpMbr in $GrpMbrs) {
Write-Host $GrpMbr.DistinguishedName -Filepath <yourfilename> -append
}
}
---
Rich Matheisen
MCSE+I, Exchange MVP
--- Rich Matheisen MCSE+I, Exchange MVP
January 7th, 2011 3:26pm
Hello.
With write-host won't work for me, so had to change your script, like that.
$DGrps = Get-DistributionGroup;
ForEach ($DGrp in $DGrps) {
$DGrp.name |Out-file -Filepath <Filename>-Append;
$GrpMbrs = Get-DistributionGroupMember -id $DGrp;
foreach ($GrpMbr in $GrpMbrs) {
$GrpMbr.DistinguishedName |Out-file -Filepath <Filename> -Append;
}
}
Free Windows Admin Tool Kit Click here and download it now
March 14th, 2011 11:06am
I know its a couple years later, but I saw this and here is what I use, very quick and simple.
Get-DistributionGroupMember -identity "group name" | ft "PrimarySMTPAddress" >c:\export-list.txt
-BV
May 9th, 2011 10:56pm
Hi All
I tested that script of O. Brazhko and Note:
We must save a filename have there's script, eg: c:\AllDistributionGroup.ps1
Body of script:
$DGrps = Get-DistributionGroup;
ForEach ($DGrp in $DGrps) {
$DGrp.name |Out-file -Filepath c:\DG.csv -Append;
$GrpMbrs = Get-DistributionGroupMember -id $DGrp;
foreach ($GrpMbr in $GrpMbrs) {
$GrpMbr.DistinguishedName |Out-file -Filepath c:\DG.csv -append;
}
}
And then we open short Exchange PowerShell run as Administrator (not check restriction permittion ).
At point [PS] ... >we can input path and file name script . eg: c:\AllDistributionGroup.ps1
wait a minutes...
We can look file result in c:\DG.csv
Good luck !
Thang.Le
Free Windows Admin Tool Kit Click here and download it now
June 17th, 2011 5:14am