Wednesday, 17 June 2015

Export Distribution List Members to CSV using Powershell

We can get Distribution list members by using the Exchange cmdlet Get-DistributionGroupMember in Powershell and export Distribution list members to CSV file using Powershell cmdlet Export-CSV.

Get Distribution Group members:

Use the following Powershell command to list Distribution list members.
Get-DistributionGroupMember -Identity <Group-Name>

Export Members of a single Distribution Group to CSV:

The following Powershell script gets members of a given distribution group and exports output to CSV file. Replace the distribution group name "TestDG" with your own group name in the below script.
$DGName = "TestDG"
Get-DistributionGroupMember -Identity $DGName | Select Name, PrimarySMTPAddress |
Export-CSV "C:\\Distribution-List-Members.csv" -NoTypeInformation -Encoding UTF8

Export All Distribution Groups and Members to CSV:

We can list all the distribution groups using Exchange cmdlet Get-DistributionGroup and get its members by passing group name into Get-DistributionGroupMember cmdlet. The following Powershell script gets all the distribution groups and its members and exports group names and members to CSV file.
$Groups = Get-DistributionGroup
$Groups | ForEach-Object {
$group = $_.Name
$members = ''
Get-DistributionGroupMember $group | ForEach-Object {
        If($members) {
              $members=$members + ";" + $_.Name
           } Else {
New-Object -TypeName PSObject -Property @{
      GroupName = $group
      Members = $members
} | Export-CSV "C:\\Distribution-Group-Members.csv" -NoTypeInformation -Encoding UTF8


1 comment:

  1. WARNING: By default, only the first 1000 items are returned. Use the ResultSize parameter to specify the number of
    items returned. To return all items, specify "-ResultSize Unlimited". Be aware that, depending on the actual number of
    items, returning all items can take a long time and consume a large amount of memory. Also, we don't recommend storing
    the results in a variable. Instead, pipe the results to another task or script to perform batch changes.