Security · 2 tools

Guest users, invitation state and last sign-in

Summary

Lists every guest with where they came from, whether they accepted the invitation and when they last signed in, so you can clean up stale guests before an audit or a tenant migration.

Implementation

Choose your tool. Your choice is remembered on other samples.

Microsoft Graph PowerShell
Connect-MgGraph -Scopes User.Read.All, AuditLog.Read.All -NoWelcome
Get-MgUser -All -Filter "userType eq 'Guest'" `
  -Property DisplayName, Mail, UserPrincipalName, CreatedDateTime, ExternalUserState, SignInActivity, CreationType |
  Select-Object DisplayName, Mail, CreatedDateTime, ExternalUserState, CreationType,
    @{ n = "Domain"; e = { ($_.Mail -split "@")[-1] } },
    @{ n = "LastSignIn"; e = { $_.SignInActivity.LastSignInDateTime } } |
  Sort-Object LastSignIn |
  Export-Csv .\guests.csv -NoTypeInformation

Prerequisites

  • Graph scopes User.Read.All and AuditLog.Read.All

Output

CSV of guests with invitation state and last sign-in.

Contributors

AuthorRole
Priyanshu UpadhyaySolution Architect, Microsoft 365 and Azure
Disclaimer. These samples are provided as is, without warranty of any kind. Read and test every script in a non-production tenant before you run it, and run reports before any command that changes or deletes data.