Export Office 365 Distribution Group Members to CSV

Using this PowerShell script you can export Office 365 Distribution Group members to CSV file along with most needed attributes like DisplayName, Group Primary SMTP Address, Group Alias, Group Type, Group Owner(Managed By), Members Count, Members, Member Email, Member’s Type, etc

5 Star
3,582 times
Add to favorites
Office 365
E-mail Twitter del.icio.us Digg Facebook
  • How to add an Attribute for a user to this script
    2 Posts | Last post February 22, 2020
    • The script is perfect. I just need to add an attribute (EmployeeNumber) for a user for the "DetailedMembersReport". I'm just getting started learning powershell,but I could really use the report soon.
      I see where it's exporting everything below. I just don't know how to get it to Also include an 
      Attribute of the user. I tried just adding in -EmployeeNumber but it's just blank., I'm quite sure 
      it's the wrong place for an attribute. 
      *thanks for whoever worked on this script. It's pretty awesome.
      #Print Detailed Output
      Function Print_Output
       if($Print -eq 1)
        $Results= New-Object PSObject -Property $Result 
        $Results | Select-Object DisplayName,PrimarySmtpAddress,Alias,Members,EmployeeNumber,MemberEmail,MemberType | Export-Csv -Path $ExportCSV -Notype -Append
    • Hi,
      Apart from the above code changes, you need to store EmployeeID in $EmployeeNumber. To get EmployeeID, you require to call the AzureAD module. So, you need to add the below code in your downloaded script.
      in line 99:
      $EmployeeNumber=(Get-AzureADUser -ObjectId $MemberEmail).ExtensionProperty["employeeId"]
      in line 216:
      After this change, you will get the desired output. If you need further assistance, let me know.
      Note: If the distribution group has another group as member, then Get-AzureADUser code will fail.
      If you want to generate report without PowerShell, I'd suggest you to try Office 365 reporting Tool by AdminDroid.
  • error when saving / accessing the file on my computer?
    3 Posts | Last post January 17, 2020
    • I have the script in my c:\test folder.
      in powershell (opened as administrator)
      I cd \test
      I run it in office 365 PS with .\getdistributiongroupmembers.ps1
      it asks for my email credentials (I am a global admin).
      It runs a bit and I get:
      Get-Content : Cannot find path 'C:\test\RecipientTypeDetails.txt' because it does not exist.
      At C:\test\getdistributiongroupmembers.ps1:221 char:22
      + ... ntTypeArray=Get-Content -Path .\RecipientTypeDetails.txt -ErrorAction ...
      +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : ObjectNotFound: (C:\test\RecipientTypeDetails.txt:String) [Get-Content], ItemNotFound
          + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
      it ends and there's no other file in the test folder.
    • i did some googling and it said to check get-location, which comes back with c:\test
      I edited line 221 and others that had .\recpient..... to "c:\test\recip...." and also wthout the quotes. Same error.  I am an admin on the local PC which is in a workgroup.
    • Hi mikeRenna,
      Do you have RecipientTypeDetails.txt in the specified folder? 
      Note: RecipientTypeDetails.txt comes along with script when you download the script zip. If the txt file is not available, please download the script again.
  • Distribution group with 1 member which is another group shows zero members.
    2 Posts | Last post December 13, 2019
    • It shows zero members for a distribution group that has only one member, what that only member is another distribution group.
      Request to add feature so that it should take into account if a member is also a distribution group. 
      For example, I have two distribution groups. Group1, and Group2. Group2 is a member and the only member of Group1. This script will show that Group1 has zero members, but it in fact has another member, that member is Group2.
      Thanks for the script!
    • Hi,
      We tested this script in our test environment(DL1 has only one member, which is DL2). The exported report is showing the correct result. Can you check again? If the issue persists, let us know.