Entra / Microsoft 365 · Users & guests
Report Entra ID guest sponsors
Report the sponsors assigned to Entra ID guest accounts.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-MgGraph -NoWelcome -Scopes User.ReadWrite.All
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
Connect-MgGraph -NoWelcome -Scopes User.ReadWrite.All[int]$Threshold = 500Write-Host "Finding guest accounts to analyze..." -ForegroundColor Green[array]$Guests = Get-MgUser -Filter "userType eq 'Guest'" -All `-Property Id, DisplayName, Sponsors, CreatedDateTime, SignInActivity, Mail -ExpandProperty Sponsors | Sort-Object DisplayNameIf (!($Guests)) {Write-Host "No guest accounts found." -ForegroundColor Red}Write-Host ("Checking {0} guest accounts..." -f $Guests.Count) -ForegroundColor Green$Report = [System.Collections.Generic.List[Object]]::new()ForEach ($Guest in $Guests) {$SponsorNames = $nullIf ($Null -eq $Guest.Sponsors.Id) {$SponsorNames = "No sponsor assigned"} Else {$SponsorNames = $Guest.Sponsors.additionalProperties.displayName -join ", "}$SignInDate = $nullIf ([string]::IsNullOrEmpty($Guest.SignInActivity.LastSuccessfulSignInDateTime)) {$SignInDate = "No sign-in activity"[int]$DaysSinceSignIn = (New-TimeSpan $Guest.CreatedDateTime).Days} Else {$SignInDate = Get-Date($Guest.SignInActivity.LastSuccessfulSignInDateTime) -format 'dd-MMM-yyyy HH:mm'[int]$DaysSinceSignIn = (New-TimeSpan $SignInDate).Days}$ReportLine = [PSCustomObject] @{Name = $Guest.DisplayNameEmail = $Guest.Mail'Sponsor Names' = $SponsorNamesCreated = Get-Date($Guest.CreatedDateTime) -format 'dd-MMM-yyyy HH:mm''Last Sign In' = $SignInDate'Days Since Sign In' = $DaysSinceSignIn.ToString()}$Report.Add($ReportLine)}$Report | Out-GridView -Title "Entra ID Guest Account Sponsors"# List all the guest accounts (and their sponsors) that haven't signed in for more than the threshold number of days$OldGuests = $Report | Where-Object {$_.'Days Since Sign In' -as [int] -gt $Threshold}Write-Host ""Write-Host ("The following guest accounts have not signed in for more than {0}} days:" -f $Threshold) -ForegroundColor RedWrite-Host ""$OldGuests | Format-Table Name, 'Sponsor Names', 'Days Since Sign In', 'Last Sign In' -AutoSize
Attribution
Author
Office365itpros