How to Get Office 365 Group Members using PowerShell?

Requirement: Get Members of Office 365 Group using PowerShell

How to Get Office 365 Group Members?
To get a list of members in Office 365 group from Microsoft 365 admin center,
  • Login to the Microsoft 365 Admin Center site: https://admin.microsoft.com
  • Expand Groups and Click on Groups link in the left navigation. 
  • Groups page lists all groups in your Office 365 tenant. Pick the desired group to list users in group.
  • This opens the group's details page which lists group members
    get members of office 365 group powershell

PowerShell to Get Group Members in Office 365:
Here is the Office 365 PowerShell get group members
#Get Credentials to connect
$Credential = Get-Credential
  
#Create the session
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ `
           -Credential $Credential -Authentication Basic -AllowRedirection
  
#Import the session
Import-PSSession $Session -DisableNameChecking

#Get all Members of Office 365 Group
Get-UnifiedGroup -Identity "Marketing@TheCrescentTech.com" | Get-UnifiedGroupLinks -LinkType Member
 
#Remove the session 
Remove-PSSession $Session 
You can export Office 365 group members using PowerShell as:
#Get all Members of Office 365 Group and export to CSV
Get-UnifiedGroup -Identity "Marketing@TheCrescentTech.com" | Get-UnifiedGroupLinks -LinkType Member `
      | Select DisplayName,PrimarySmtpAddress | Export-CSV "C:\Temp\GroupMembers.csv" -NoTypeInformation

Export Office 365 Group Members to CSV using PowerShell
Let's export all members of all Office 365 groups 
$CSVPath = "C:\Temp\AllGroupMembers.csv"

#Get Credentials to connect
$Credential = Get-Credential
  
#Create the session
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ `
           -Credential $Credential -Authentication Basic -AllowRedirection
  
#Import the session
Import-PSSession $Session -DisableNameChecking

#Remove the CSV file if exists
If(Test-Path $CSVPath) { Remove-Item $CSVPath}

#Get All Office 365 Groups
$O365Groups=Get-UnifiedGroup
ForEach ($Group in $O365Groups) 
{ 
    Write-Host "Group Name:" $Group.DisplayName -ForegroundColor Green
    Get-UnifiedGroupLinks –Identity $Group.Id –LinkType Members | Select DisplayName,PrimarySmtpAddress

    #Get Group Members and export to CSV
    Get-UnifiedGroupLinks –Identity $Group.Id –LinkType Members | Select-Object @{Name="Group Name";Expression={$Group.DisplayName}},`
         @{Name="User Name";Expression={$_.DisplayName}}, PrimarySmtpAddress | Export-CSV $CSVPath -NoTypeInformation -Append
}
 
#Remove the session 
Remove-PSSession $Session

Get Office 365 Group Members using Azure AD PowerShell Module 
You can also list group users using Azure AD PowerShell as:
#Get Credentials to connect
$Cred = Get-Credential

#Connect to AzureAD
Connect-AzureAD -Credential $Cred

#Get the Group
$Group = Get-AzureADGroup -Filter "DisplayName eq 'HR Team'"

#Get Group Members
Get-AzureADGroupMember -ObjectId $Group.ObjectId
How to Get Office 365 Group Members using PowerShell? How to Get Office 365 Group Members using PowerShell? Reviewed by Salaudeen Rajack on April 07, 2019 Rating: 5

1 comment:

  1. Your blog has been a tremendous resource for us!

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.