October 2, 2018

Getting Distribution Group Members

by Steve G

3 min read

No Comments

So, your organization moved to office 365 and you have crossed your faithful years of managing its IT infrastructure, suddenly you are into the cloud generation now. Everyone talks about security which makes your security auditor ask constant reports about group membership details. If you are this person and does not deal the traditional way of logging into office 365 for reports, instead by using the extensive powershell options that suits your own customizations, then you are in the right page. Let me explain the below script #DroidWay 😉 .

Below given script explains how to export distribution (list)group members in office 365. The Get distribution group members powershell office 365 cmdlet is used to retrieve the members from Office 365 distribution list. Office 365 distribution list management can be a easy task with the help of cmdlets available in powershell, with a variety of supported actions such as Office 365 distribution group management, Export group members to csv from Office 365, etc,.


If you need to export Distribution group members along with more useful attributes and advanced filters/customization, please refer our Detailed Office 365 Distribution Group Membership Report blog.



Save the above script(example: script.ps1), execute it as script.ps1 -O365Username admin@Gen.onmmicrosoft.com -O365password StrangePwd  (OR) Just execute script.ps1 and type the O365 credentials in the UI.


This blog explains few powershell scripting techniques that can be used to Export list of distribution group members from Office 365 to csv file .


Step1:  We have to declare argument parameters that will be specified as input during script execution, in this Example I have used the Office 365 username and password.



Step2: Create powershell session for Office 365 and import it for use.


Step3: Declare parameters that will store the Export CSV Output , Get-group will get the names of all Office365  Distribution groups, which is stored in $name to For loop to Get-distributionGroupmember users from it, which is further to be stored in $member. Mention the file location of output file (Empty .CSV file) where you need the Office365 distribution group members list to be exported in $ExportCSV .



Step4: Add another For loop within the previous Step3 For loop to receive each Office 365 Distribution Group member($member) of the Office 365 distribution list($name) and store it in two variables enclosed within the $details parameter. Create New object for each $details and add it into the $results object.



Step5: Export the results and end the powershell session.




Sample OUTPUT CSV Content:


“Alex Wilber”,”Tailspin Toys”
“Joni Sherman”,”Tailspin Toys”
“Megan Bowen”,”Tailspin Toys”
“Lynne Robbins”,”Tailspin Toys”
“Henrietta Mueller”,”Tailspin Toys”
“Lidia Holloway”,”Tailspin Toys”
“Joni Sherman”,”Executives”
“Henrietta Mueller”,”Executives”
“Joni Sherman”,”Legal Team”
“Lynne Robbins”,”Finance Team”
“Alex Wilber”,”Sales Team”
“Megan Bowen”,”Sales Team”
“Henrietta Mueller”,”Sales Team”
“Lidia Holloway”,”Sales Team”
“Alex Wilber”,”Northwind Traders”
“Joni Sherman”,”Northwind Traders”
“Megan Bowen”,”Northwind Traders”

Share article