Get a list of users from all groups in Office 365 | Msol

GET A LIST OF ALL USERS FROM ALL GROUPS IN OFFICE 365

We can create many different types of groups in Office 365/Azure. The different types that get created are Distribution Lists, Mail-Enabled Security Groups, Unified Groups (New Office 365 Groups), Security Groups, Dynamic Security Groups etc. I have seen tenants that have hundreds of different types of groups. If we are to get a report of all the members within those groups, we would need to run multiple PowerShell commands, one for each group type.

Here is a snippet of the list of different types of groups and lists that can be created in the Exchange Admin Center. If you have a lot of these groups, you could either get a list of each group buy playing with the commands above or use the Azure Module to get a list of all MSOL Groups. Get-msolgroup or Get-AzureASMSGroup

https://o365inside.com/wp-content/uploads/2018/09/word-image-216.png

I have written a script that is useful in getting all groups and members in O365. The script gets a list of all groups in Office 365, and then cycles through each group getting their members.

If you would just like a list of groups without their members you can download a .csv file from the portal.

https://o365inside.com/wp-content/uploads/2018/09/word-image-217.png

Script Execution:

  1. Run PowerShell as Admin and connect to MSOL service by running Connect-MsolService. For details on how to connect to MSOL reference the link: Connect to Office 365 PowerShell: https://docs.microsoft.com/en-us/office365/enterprise/powershell/connect-to-office-365-powershell
  2. You will first be prompted for a location to save the output file
  3. You will get the following results in the output: Group DisplayName, Group Email, Member DisplayName, and Member Email
  4. This is what the .csv file would look like:

About the Script. (how does it work):

For this is have skipped the beautification e.g. Adding the choice button, headers, write and read host etc.

  • The script begins by collecting the location for the .csv file (where would you like to save the data). Then I begin by collecting a list of groups and saving it in a variable “$objGroups”
$objGroups = Get-msolgroup -All | Sort-object objectid
  • I then cycle though each group, get a list of members and save it in a variable
    “$objGMembers”. Since MSOL Groups are addressed by their ObjectID the groups ObjectID is called.
    Foreach ($objGroup in $objGroups)
    {
    write-host "Processing $($objGroup.DisplayName)..."
    $objGMembers = Get-MsolGroupMember -groupobjectid $($objGroup.objectid)
    write-host "Found $($objGMembers.Count) members..."
    $name = $_.objectid
    $displayname = $_.displayname
    $email = $_.proxyaddresses
  • Once all the data is collected I go ahead and write the output to a file and write the output to PowerShell for visibility.
Foreach ($objMember in $objGMembers)
{
Out-File -FilePath $OutputFile -InputObject "$($objGroup.DisplayName),$($objGroup.proxyaddresses),$($objMember.DisplayName),$($objMember.EmailAddress)" -Encoding UTF8 -append
write-host "`t$($objGroup.DisplayName),$($objGroup.proxyaddresses),$($objMember.DisplayName),$($objMember.EmailAddress)"

Download the Script:

Technet: Get a list of all users from all groups in Office 365

Reference:

Manage Distribution Groups and Distribution Group Members
Manage mail-enabled security groups
Manage Office 365 Groups with PowerShell
Manage Dynamic Distribution Groups