Entra / Microsoft 365 · Groups
Find who added guests to groups
A script to find and report who added new guest members to Microsoft 365 Groups (and Teams) over the last 90 days.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
# Review required modules and connection steps before running.# Connect to Microsoft Graph or Exchange Online as needed for this script.
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
param([int] $LookbackDays = 90,[string] $StartDate = (Get-Date).AddDays(-$LookbackDays),[string] $EndDate = (Get-Date).AddDays(1); $StartDate = (Get-Date).AddDays(-$LookbackDays); $NewGuests = 0)$Records = (Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -Operations "Add Member to Group" -ResultSize 2000 -Formatted)If ($Records.Count -eq 0) {Write-Host "No Group Add Member records found." }Else {Write-Host "Processing" $Records.Count "audit records..."$Report = @()ForEach ($Rec in $Records) {$AuditData = ConvertFrom-Json $Rec.Auditdata# Only process the additions of guest users to groupsIf ($AuditData.ObjectId -Like "*#EXT#*") {$TimeStamp = Get-Date $Rec.CreationDate -format g# Try and find the timestamp when the Guest account was created in AADTry {$AADCheck = (Get-Date(Get-AzureADUser -ObjectId $AuditData.ObjectId).RefreshTokensValidFromDateTime -format g) }Catch {Write-Host "Azure Active Directory record for" $AuditData.ObjectId "no longer exists" }If ($TimeStamp -eq $AADCheck) { # It's a new record, so let's write it out$NewGuests++$ReportLine = [PSCustomObject][Ordered]@{TimeStamp = $TimeStampUser = $AuditData.UserIdAction = $AuditData.OperationGroup = $AuditData.modifiedproperties.newvalue[1]Guest = $AuditData.ObjectId }$Report += $ReportLine }}}}Write-Host $NewGuests "new guest records found..."$Report | Sort GroupName, Timestamp | Get-Unique -AsString | Format-Table Timestamp, Groupname, Guest
Parameters
ParameterDefaultNotes
-LookbackDays90Number of days to look back for guest member additions to Microsoft 365 groups and Teams.-StartDate(Get-Date).AddDays(-90)Start of the audit log search window.-EndDate(Get-Date).AddDays(1)End of the audit log search window.Attribution
Author
Office365itpros