Thursday, 26 April 2018

Export Office 365 Distribution Group Members to CSV using PowerShell

This post will help you to find and export distribution group members in Office 365 by using powershell script. We can list all the office 365 distribution lists by using the Exchange online powershell cmdlet Get-DistributionGroup and its group members by Get-DistributionGroupMember cmdlet.

Before proceed run the following command to connect Exchange Online powershell module.
$365Logon = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $365Logon -Authentication Basic -AllowRedirection
Import-PSSession $Session
Run the following command to list all the distribution groups.
Get-DistributionGroup -ResultSize Unlimited
The following command lists all the members for the given distribution list.
Get-DistributionGroupMember -Identity "<group name>" -ResultSize Unlimited

Export Members of a Single Distribution List to CSV:

The following powershell script gets members of a given distribution group and export members list to CSV file. You can replace the parameter <group name> with your own group name in the below script.
$DGName = "<group name>"
Get-DistributionGroupMember -Identity $DGName -ResultSize Unlimited | Select Name, PrimarySMTPAddress, RecipientType |
Export-CSV "C:\\Distribution-Group-Members.csv" -NoTypeInformation -Encoding UTF8

Export All Distribution Groups and Members List to CSV:

First we can get all the distribution groups by using Get-DistributionGroup cmdlet and retrieve its members by iterating every group with Get-DistributionGroupMember cmdlet. The following script exports all the distribution lists and their memberships to CSV file.
$groups = Get-DistributionGroup -ResultSize Unlimited
$totalmbx = $groups.Count
$i = 1 
$groups | ForEach-Object {
Write-Progress -activity "Processing $_.DisplayName" -status "$i out of $totalmbx completed"
$group = $_
Get-DistributionGroupMember -Identity $group.Name -ResultSize Unlimited | ForEach-Object {
$member = $_
$Result += New-Object PSObject -property @{ 
GroupName = $group.DisplayName
Member = $member.Name
EmailAddress = $member.PrimarySMTPAddress
RecipientType= $member.RecipientType
$Result | Export-CSV "C:\\All-Distribution-Group-Members.csv" -NoTypeInformation -Encoding UTF8

CSV output of O365 Distribution Groups and Members:

Export Office 365 Distribution Groups and their Memberships to CSV


No comments:

Post a Comment