in Office 365 you get three types of groups: distribution groups, dynamic distribution groups, and Office 365 groups (unified groups)…

The first two of those are based on Exchange Server and are very straight and easy to use for anyone who is familiar with Exchange Server… However the third type might look something new to them despite being at the same level of ease and straight forward!

When using Exchange Online PowerShell to list the groups, there are three commands to use for this:

Get-DistributionGroup
Get-DynamicDistributionGroup
Get-UnifiedGroup

Similar to the first two commands, Get-UnifiedGroup works the same way… You will get the group, then check its members attribute or property to get the other member users…

Below is a small script that I made will do the required job for you. Once the process is completed, you will get a file named O365GroupMembers.csv contains four columns!

$output = @()
$groups = get-unifiedgroup | select DisplayName,PrimarySmtpAddress,AccessType
$groups | ft -au
foreach ($group in $groups) {
    Write-Host " == Working on" $group.DisplayName"," $group.PrimarySmtpAddress
    $members = Get-UnifiedGroupLinks $group.PrimarySmtpAddress -linktype members -resultsize unlimited
    foreach ($member in $members) {
        $userItem = new-object PSObject
        $userItem | Add-Member NoteProperty -Name "GroupName" -Value $group.DisplayName
        $userItem | Add-Member NoteProperty -Name "GroupEmail" -Value $group.PrimarySmtpAddress
        $userItem | Add-Member NoteProperty -Name "AccessType" -Value $group.AccessType
        $userItem | Add-Member NoteProperty -Name "UserEmail" -Value $member.PrimarySmtpAddress
        $output += $userItem
    }
}
$output | Export-csv -Path O365GroupMembers.csv -NoTypeInformation

You need to copy the script into a .ps1 file, then run it from a machine that is connected to the Office 365 using PowerShell. Note that you might want to change the execution policy on the machine that you are running the script on…

The output file will be saved in the same location from where you open the PowerShell prompt and run the script, so make sure you are on an appropriate path before running the script.

Hope you find it useful as I did!

Categories:

2 Responses

  1. Great script, but When I change from Get-UnifiedGroup to Get-DistributionGroup I get an error. What changes need to be made for it to run correctly?

    • Hi,

      If you mean that you changed the command in line 2 of the script above, then I believe it is something to do with the properties that the distribution groups have. I do not believe they contain a property named “AccessType” and no UnifiedGroupLinks property as well that is related to the classic distribution groups… Basically that whole script is made to work with unified groups not the classic distribution groups.

      For classic distribution groups it is much simpler, you can use the below to get the list of classical distribution groups into a CSV file:

      $output = @()
      $groups = Get-DistributionGroup -resultsize unlimited | select Name,PrimarySmtpAddress
      $groups | ft -au
      foreach ($group in $groups) {
      Write-Host "  == Working on" $group.Name"," $group.PrimarySmtpAddress
          $members = Get-DistributionGroupMember $group.PrimarySmtpAddress -resultsize unlimited | select primarysmtpaddress
          foreach ($member in $members) {
              $userItem = new-object PSObject
              $userItem | Add-Member NoteProperty -Name "GroupName" -Value $group.Name
              $userItem | Add-Member NoteProperty -Name "GroupEmail" -Value $group.PrimarySmtpAddress
              $userItem | Add-Member NoteProperty -Name "UserEmail" -Value $member.primarysmtpaddress
              $output += $userItem
          }
      }
      $output | Export-csv -Path StdDistGroupMembers.csv -NoTypeInformation

      If you are trying to type the command directly in the powershell line while you have a connection to the Office 365 powershell then it might be something for roles and security, since the security are role based and sometimes not all of the cmdlets are available for all users.

      Worth also to double-check the execution policy…

      Hope these helps 🙂

Leave a Reply to SalehCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.