Unified group members

Roger Roger 4,541 Reputation points
2022-06-09T18:17:52.657+00:00

Hello

i have users in csv file. i want to get the unified groups to which they are members of. will the below script fetch me the information.

$names= Import-Csv C:\data.csv  
$Result= @()   
$groups= Get-UnifiedGroup -ResultSize Unlimited  
       foreach($UnifiedGroup in $UnifiedGroups){  
           foreach($member in (Get-UnifiedGroupLinks -Resultsize Unlimited $UnifiedGroup.name -LinkType Members)){  
               if ($member.PrimarySMTPaddress -eq $name.name){  
                   $Result1 = New-Object PSObject   
                   $Result1 | Add-Member NoteProperty -Name "User" -Value $name.name  
                   $Result1 | Add-Member NoteProperty -Name "UnifiedGroup" -Value (Get-distributiongroup -Resultsize Unlimited $group.name).PrimarySMTPaddress  
                   $Result += $Result1   
               }  
           }  
       }      
   }  
   $Result | Export-csv -Path C:\temp\output.csv -NoTypeInformation  
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
2,904 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
6,096 questions
{count} votes

Accepted answer
  1. Joyce Shen - MSFT 16,331 Reputation points Microsoft Employee
    2022-06-13T05:08:55.347+00:00

    Hi @Roger Roger

    What about checking this:

    $names= Import-Csv C:\test\data.csv  
    $Result= @()  
    foreach($name in $names){  
    $groups= Get-UnifiedGroup -ResultSize Unlimited  
            foreach($UnifiedGroup in $groups){  
                foreach($member in (Get-UnifiedGroupLinks -Resultsize Unlimited $UnifiedGroup.name -LinkType Members)){  
                    if ($member.PrimarySMTPaddress -eq $name.User_Principal_Name){  
                        $Result1 = New-Object PSObject   
                        $Result1 | Add-Member NoteProperty -Name "User" -Value $name.User_Principal_Name  
                        $Result1 | Add-Member NoteProperty -Name "UnifiedGroup" -Value (Get-UnifiedGroup -Resultsize Unlimited $UnifiedGroup.name).PrimarySMTPaddress  
                        $Result += $Result1  
                    }  
                }  
            }      
    }  
      
     $Result | Export-csv -Path C:\test\output.csv -NoTypeInformation  
    

    210668-image.png
    210616-image.png


    If an Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments

2 additional answers

Sort by: Most helpful
  1. Joyce Shen - MSFT 16,331 Reputation points Microsoft Employee
    2022-06-10T02:57:52.75+00:00

    Hi @Roger Roger

    For checking one user in which O365 group you could use the script below:
    $UserEmail= "user@keyman .com"
    $Mailbox = Get-Mailbox | Where {$.PrimarySmtpAddress -eq $UserEmail}
    $Office365GroupsMember = Get-UnifiedGroup | where { (Get-UnifiedGroupLinks $
    .Alias -LinkType Members | foreach {$_.name}) -contains $mailbox.Alias}
    $Office365GroupsMember

    I tried your command in my environment cannot work, you may need to modify the variable correctly.
    210161-image.png

    Or you could use the script provided here to meet your requirement:
    List All the Distribution Groups a User Is Member of Using PowerShell
    Please Note: Since the web site is not hosted by Microsoft, the link may change without notice. Microsoft does not guarantee the accuracy of this information.


    If an Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments

  2. Roger Roger 4,541 Reputation points
    2022-06-12T05:08:49.03+00:00

    I have used the below syntax but i getting blank output. The user is member of few unified groups. on two unified groups i am aware he is member but i am getting blank output.

    $names= Import-Csv C:\data.csv  
     $Result= @()   
     $groups= Get-UnifiedGroup -ResultSize Unlimited  
            foreach($UnifiedGroup in $UnifiedGroups){  
                foreach($member in (Get-UnifiedGroupLinks -Resultsize Unlimited $UnifiedGroup.name -LinkType Members)){  
                    if ($member.PrimarySMTPaddress -eq $name.name){  
                        $Result1 = New-Object PSObject   
                        $Result1 | Add-Member NoteProperty -Name "User" -Value $name.name  
                        $Result1 | Add-Member NoteProperty -Name "UnifiedGroup" -Value (Get-distributiongroup -Resultsize Unlimited $group.name).PrimarySMTPaddress  
                        $Result += $Result1   
                    }  
                }  
        }  
        $Result | Export-csv -Path C:\temp\output.csv -NoTypeInformation  
    
    No comments