Retrieve Distribution Group Members in Office 365 Exchange Online

Introduction

This script shows how to use the Exchange Web Service Managed API to retrieve group members in distribution groups. Additionally, this script enables you to easily retrieve group members if you have nested distribution groups.

Scenario

Retrieval of distribution group members in Office 365 Exchange Online is a common maintenance job for Office 365 administrators. This information is needed for varieties of reasons. For example, administrators may want to compare two email formats of distribution group members before you apply new email formats.

Prerequisites

This script requires Exchange Web Service Managed API 2.0. Please download and install the package from Microsoft Download Center. This script cannot work correctly without this package.

Script

This script contains the following advanced functions:

You can use this script in the following way:

  1. Open Windows PowerShell.
  2. Run Import-Module cmdlet to import this module file.
    Import-Module filepath\scriptname.psm1
Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.
PowerShell
Edit|Remove
#Get properties for each distribution group member and write output$result = New-Object System.Management.Automation.PSObject 
$result|Add-Member-MemberType NoteProperty -Name "DistributionGroupAddress"-Value $Identity$result|Add-Member-MemberType NoteProperty -Name "MemberAddress"-Value $groupMember.Address 
if ($AllProperty) { 
    $properties = $groupMember.GetType().GetProperties() 
    foreach($propertyin$groupMember.GetType().GetProperties()) { 
        $propertyName = $property.Name 
        if ($propertyName-ne "Address") { 
            $result|Add-Member-MemberType NoteProperty -Name $property.Name -Value $groupMember.$propertyName 
        } 
    } 
} 
$PSCmdlet.WriteObject($result)

Examples

Example 1: How to display help about Get-OSCEXODistributionGroupMember.
To display help about this function, run the following command:
Get-Help Get-OSCEXODistributionGroupMember -Full
 
Example 2: How to initiate a connection to Office 365 Exchange Online.
To initiate a connection to Office 365 Exchange Online, please run this command. You must run this example before any other step:
Connect-OSCEXOWebService -Credential (Get-Credential admin@domain01.onmicrosoft.com)
 
Note The Connect-OSCEXOWebService function creates a new variable called exService. This variable is in the global scope of the current Windows PowerShell session. This variable is used by other functions in the script.
 
Example 3: How to retrieve group members from specified distribution group.
To retrieve group members in specified distribution group, please run this command.
Get-OSCEXODistributionGroupMember -Identity "corphr@domain01.onmicrosoft.com"
  
Example 4: How to retrieve the email addresses of distribution groups from pipeline, then return members from the distribution groups and nested distribution groups.
To retrieve the email addresses of distribution groups from pipeline, then return members from the distribution groups and nested distribution groups, please run this command:
"corphr@domain01.onmicrosoft.com" | Get-OSCEXODistributionGroupMember -Recurse -AllProperty | Format-Table -AutoSize
 
Example 5: How to retrieve group members from specified distribution group and export the list to a comma-separated values file.
To retrieve group members from specified distribution group and export the list to a comma-separated values file, please run this command:
Get-OSCEXODistributionGroupMember -Identity "corphr@domain01.onmicrosoft.com" | Export-Csv -Path .\groupmembers.csv -NoTypeInformation
 

Additional Resources

Technical Resources:
Windows PowerShell Advanced Function
Expanding a distribution list by using the EWS Managed API

Forum Threads:
http://community.office365.com/en-us/forums/146/p/50964/178298.aspx
http://community.office365.com/en-us/forums/150/p/70090/262371.aspx