Entra / Microsoft 365 · Groups
Report membership M365 group
Report the membership of a Microsoft 365 Group.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-ExchangeOnline -ShowBanner:$false
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
CLS# Check that we are connected to Exchange Online$ModulesLoaded = Get-Module | Select NameIf (!($ModulesLoaded -match "ExchangeOnlineManagement")) {Write-Host "Please connect to the Exchange Online Management module and then restart the script"; break}$OrgName = (Get-OrganizationConfig).Name$Version = "1.0"$ReportFile = "c:\temp\M365MemberReport.html"$CSVFile = "c:\temp\M365MemberReport.csv"$CheckGroup = Read-Host "Enter the Microsoft 365 group to check"$Group = Get-UnifiedGroup -Identity $CheckGroupIf (!($Group)) { Write-Host "Sorry - can't find the" $CheckGroup "group; exiting..." ; break}$DisplayName = $Group.DisplayName$CreationDate = Get-Date $Group.WhenCreated -format g$MemberList = [System.Collections.Generic.List[Object]]::new()$Members = Get-UnifiedGroupLinks -LinkType Members -Identity $Group.ExternalDirectoryObjectIdForEach ($M in $Members) {$UserType = "Tenant account"If ($M.WindowsLiveID -like "*#EXT#*") { $UserType = "Guest account" }$MemberLine = [PSCustomObject][Ordered]@{Name = $M.DisplayNameUPN = $M.WindowsLiveIDEmail = $M.PrimarySmtpAddressTitle = $M.TitleDepartment = $M.DepartmentOffice = $M.OfficeCity = $M.CityPostCode = $M.PostalCodeCountry = $M.CountryOrRegionType = $UserTypeMemberType = "Member" }$MemberList.Add($MemberLine)}$Owners = Get-UnifiedGroupLinks -LinkType Owners -Identity $Group.ExternalDirectoryObjectIdForEach ($O in $Owners) {$UserType = "Tenant account"$OwnersCount++If ($O.WindowsLiveID -like "*#EXT#*") { $UserType = "Guest account" }$MemberLine = [PSCustomObject][Ordered]@{Name = $O.DisplayNameUPN = $O.WindowsLiveIDEmail = $O.PrimarySmtpAddressTitle = $O.TitleDepartment = $O.DepartmentOffice = $O.OfficeCity = $O.CityPostCode = $O.PostalCodeCountry = $O.CountryOrRegionType = $UserTypeMemberType = "Owner" }$MemberList.Add($MemberLine)}# Create the HTML report$htmlhead="<html><style>BODY{font-family: Arial; font-size: 8pt;}H1{font-size: 22px; font-family: 'Segoe UI Light','Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;}H2{font-size: 18px; font-family: 'Segoe UI Light','Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;}H3{font-size: 16px; font-family: 'Segoe UI Light','Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;}TABLE{border: 1px solid black; border-collapse: collapse; font-size: 8pt;}TH{border: 1px solid #969595; background: #dddddd; padding: 5px; color: #000000;}TD{border: 1px solid #969595; padding: 5px; }td.pass{background: #B7EB83;}td.warn{background: #FFF275;}td.fail{background: #FF2626; color: #ffffff;}td.info{background: #85D4FF;}</style><body><div align=center><p><h1>Microsoft 365 Groups Membership Listing</h1></p><p><h2><b>$DisplayName</b></h2></p><p><h3>Generated: " + (Get-Date -format g) + "</h3></p></div>"$htmlbody = $MemberList | ConvertTo-Html -Fragment$htmltail = "<p>Report created for: " + $OrgName + "</p>" +"<p>Group name: " + $DisplayName + "</p>" +"<p>"+ $Group.Notes +"</p>" +"<p>Created: " + $CreationDate + "<p>" +"<p>-----------------------------------------------------------------------------------------------------------------------------</p>"+"<p>Number of members: " + $Group.GroupMemberCount + "</p>" +"<p>Number of guests: " + $Group.GroupExternalMemberCount + "<p>"+"<p>Number of owners: " + $OwnersCount + "</p>" +"<p>-----------------------------------------------------------------------------------------------------------------------------</p>"+"<p>Microsoft 365 Group Membership Report <b>" + $Version + "</b>"$htmlreport = $htmlhead + $htmlbody + $htmltail$htmlreport | Out-File $ReportFile -Encoding UTF8$MemberList | Export-CSV -NoTypeInformation $CSVFile$MemberList| Out-GridView
Attribution
Author
Office365itpros