Command shell script to get list of distribution groups and its members

Here is a command shell script to generate the list of distribution group and their members.

Thanks to Michael B. Smith for this script. 

 

Copy the content to a notepad and save the file as Report-DistributionGroupsAndMembers.ps1

Open command shell and go to the folder where the file is saved and run the following command:

Report-DistributionGroupsAndMembers.ps1 > DGList.txt

 

## SCRIPT START

## Report-DistributionGroupsAndMembers.ps1

## v1.1

##

## Michael B. Smith

## http://TheEssentialExchange.com

## August, 2010

##

## Requires Exchange Management Shell

## Should work with either PowerShell v1 or v2

## Tested on both Exchange 2007 and Exchange 2010

##

function formatManager($formatstring, $manager)

{

 $formatstring -f $manager.Name, ($manager.Parent.ToString() [plus] "/" [plus]  $manager.RDN.ToString().SubString(3))

}

Get-DistributionGroup -ResultSize Unlimited |% {

 $group = $_

 "Group Name & Identity: {0}, {1}" -f $group.Name, $group.Identity

 $managedBy = $group.ManagedBy

 if( $managedBy -is [Microsoft.Exchange.Data.Directory.ADObjectId] )

 {

  formatManager "Group manager: {0}, {1}" $managedBy

 }

 elseif( $managedBy.Count -gt 1 )

 {

  [bool]$first = $true

  foreach( $manager in $managedBy ) 

  {

   if( $first ) 

   {

    formatManager "Group managers: {0}, {1}" $manager

    $first = $false

   }

   else

   {

    formatManager "                {0}, {1}" $manager

   }

  }

 }

 elseif( $managedBy.Count -gt 0 )

 {

  formatManager "Group manager: {0}, {1}" $managedBy[0]

 }

 "Members:"

 Get-DistributionGroupMember -Identity $group.Identity -ResultSize Unlimited |% {

  foreach( $member in $_ )

  {

   "`t$($member.Name)"

  }

 }

 "---"

}

## SCRIPT END

The URL for the original script is

http://theessentialexchange.com/blogs/michael/archive/2010/08/18/generating-a-report-on-distribution-groups-and-their-membership.aspx