Wednesday, 17 June 2015

Get Exchange Distribution List Members using Powershell

We can get and list Distribution group members using the Exchange Powershell cmdlet Get-DistributionGroupMember. In this article, I am going to write Powershell script to get distribution list members, export distribution list members to CSV file and export distribution groups and members to CSV file.

Get Distribution List members:

Run the following command to enable Exchange cmdlets if you are working with normal PowerShell console instead of Exchange Management Shell.
Add-PSSnapin *Exchange*
Use the following Powershell command to list Distribution group members.
Get-DistributionGroupMember -Identity <Group-Name>
By default this command returns only two properties (Name and RecipientType) of the Group members. You can select required values by using select command.
Get-DistributionGroupMember -Identity <Group-Name> | Select Name, RecipientType, PrimarySMTPAddress

Export Distribution List Members to CSV:

The following Powershell script gets a list of distribution group members 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 Distribution Groups and Members to CSV:

The following PowerShell script gets a list of Distribution groups and its members and exports output 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


No comments:

Post a Comment