Dato che nei nostri tenant stanno iniziando ad essere presenti sempre più utenti guest, spesso perché in passato non sono mai state applicate limitazioni o controlli adeguati, questo script permette di recuperare in modo rapido, completo e automatizzato tutte le informazioni fondamentali su questi account esterni. Analizza ogni guest in profondità, estraendo dati chiave come identità, indirizzi email, stato dell’account, data di creazione, accettazione dell’invito, ultime attività di accesso, dominio di provenienza, gruppi a cui appartiene e utente che lo ha invitato.
Tutte queste informazioni vengono organizzate in un report chiaro, ordinato e pronto per l’audit, trasformando un’attività lunga e manuale in un processo immediato, preciso e indispensabile per mantenere il tenant sicuro e sotto controllo.
<#
Prerequisiti:
avere il modulo di Microsoft Graph PowerShell installato, se non fosse presente usare:
Install-Module Microsoft.Graph -Scope AllUsers
Avere un account con permessi adeguati per poter accedere alla lettura dei dati delle utenze guest
#>
Write-Host "Connecting to Microsoft Graph..." -ForegroundColor Cyan
Connect-MgGraph -Scopes "User.Read.All","Directory.Read.All","AuditLog.Read.All","Group.Read.All"
Write-Host "Retrieving guest users..." -ForegroundColor Cyan
$Guests = Get-MgUser -Filter "userType eq 'Guest'" -All `
-Property "id,displayName,userPrincipalName,mail,accountEnabled,createdDateTime,externalUserState,externalUserStateChangeDateTime,signInActivity,createdBy"
$Results = @()
foreach ($Guest in $Guests) {
Write-Host "Processing $($Guest.DisplayName)..." -ForegroundColor Yellow
# --- Domain extraction ---
$UPNDomain = $null
if ($Guest.UserPrincipalName -match "@") {
$UPNDomain = $Guest.UserPrincipalName.Split("@")[1]
}
# --- MemberOf (group list) ---
$Groups = Get-MgUserMemberOf -UserId $Guest.Id -All -ErrorAction SilentlyContinue
$GroupNames = $Groups | ForEach-Object {
if ($_.AdditionalProperties.displayName) {
$_.AdditionalProperties.displayName
}
}
$GroupList = $GroupNames -join "; "
# --- InvitedByUser ---
$InvitedBy = $null
if ($Guest.CreatedBy -and $Guest.CreatedBy.User) {
$InvitedBy = $Guest.CreatedBy.User.DisplayName
}
# --- Last Sign-in ---
$LastSignIn = $null
if ($Guest.SignInActivity) {
$LastSignIn = $Guest.SignInActivity.LastSignInDateTime
}
# --- Build output object ---
$Results += [PSCustomObject]@{
DisplayName = $Guest.DisplayName
UserPrincipalName = $Guest.UserPrincipalName
Mail = $Guest.Mail
AccountEnabled = $Guest.AccountEnabled
CreatedDateTime = $Guest.CreatedDateTime
ExternalUserState = $Guest.ExternalUserState
ExternalUserStateChangeDateTime = $Guest.ExternalUserStateChangeDateTime
LastSignInDateTime = $LastSignIn
UserPrincipalNameDomain = $UPNDomain
MemberOf = $GroupList
InvitedByUser = $InvitedBy
}
}
# --- Ensure C:\temp exists ---
$OutputFolder = "C:\temp"
if (-not (Test-Path -Path $OutputFolder)) {
Write-Host "Creating folder C:\temp..." -ForegroundColor Cyan
New-Item -ItemType Directory -Path $OutputFolder | Out-Null
}
# --- Export to CSV ---
$OutputPath = "C:\temp\GuestUsersAudit.csv"
$Results | Export-Csv -Path $OutputPath -NoTypeInformation -Encoding UTF8
Write-Host "CSV generated successfully: $OutputPath" -ForegroundColor Green