Wednesday, 30 September 2015

List Distribution Group Members in office 365 Powershell

The article helps you to list distribution group members from office 365 by using powershell script. We can list distribution groups by using the powershell cmdlet Get-DistributionGroup and get distribution group members by using the cmdlet Get-DistributionGroupMember.

Note: Before proceed, Connect Exchange Online Remote PowerShell.

The following command lists all the office 365 distribution groups.
Get-DistributionGroup | Select DisplayName,GroupType,PrimarySmtpAddress

List Office 365 Distribution Group Members

Use the below powershell command to select members of single distribution group.
Get-DistributionGroupMember -Identity '[group_name]'
If you want to list members of all the cloud exchange distribution groups, first, we need to get the results of the Get-DistributionGroup cmdlet to a variable. Then we can pipe the variable to ForEach-Object and get members for all the distribution groups.
$Groups = Get-DistributionGroup
$Groups | ForEach-Object {
$group = $_.Name
Get-DistributionGroupMember $group | ForEach-Object {
      New-Object -TypeName PSObject -Property @{
       Group = $group
       Member = $_.Name
       EmailAddress = $_.PrimarySMTPAddress
       RecipientType= $_.RecipientType
}}}

Export All Distribution Group Members to CSV

We can export powershell output into CSV file using Export-CSV cmdlet. The following command exports all the office 365 distribution group members to CSV file.
$Groups = Get-DistributionGroup
$Groups | ForEach-Object {
$group = $_.Name
Get-DistributionGroupMember $group | ForEach-Object {
      New-Object -TypeName PSObject -Property @{
       Group = $group
       Member = $_.Name
       EmailAddress = $_.PrimarySMTPAddress
       RecipientType= $_.RecipientType
}}} |

Export-CSV "C:\\DistributionGroupMembers.csv" -NoTypeInformation -Encoding UTF8

Advertisements
Advertisements

No comments:

Post a Comment