Guest users help organizations collaborate with external vendors, partners, and consultants. However, inactive guest accounts often remain in Microsoft 365 long after they are needed.
These stale guest accounts can:
👉 In this guide, you’ll learn how to identify and remove inactive Microsoft 365 guest users using Graph PowerShell safely and efficiently.
By the end of this article, you’ll be able to:
Install-Module Microsoft.Graph -Scope CurrentUser
The following permissions are required:
Admin consent may be required.
This solution uses the signInActivity property to determine inactivity.
The signInActivity property is available in:
❌ Limited or Unavailable In
⚠️ Important Notes
Connect-MgGraph -Scopes `
"User.Read.All",
"User.ReadWrite.All",
"Directory.ReadWrite.All",
"AuditLog.Read.All"
This script identifies guest users inactive for the last 90 days.
# Define inactivity threshold
$DaysInactive = 90
$ThresholdDate = (Get-Date).AddDays(-$DaysInactive)
# Retrieve guest users
$Guests = Get-MgUser `
-Filter "userType eq 'Guest'" `
-Property DisplayName,UserPrincipalName,SignInActivity `
-All
# Find inactive guests
$InactiveGuests = $Guests | Where-Object {
$_.SignInActivity.LastSuccessfulSignInDateTime -lt $ThresholdDate
}
# Display results
$InactiveGuests | Select-Object `
DisplayName,
UserPrincipalName,
@{Name="LastSignIn";Expression={$_.SignInActivity.LastSuccessfulSignInDateTime}}
Sample Output
| DisplayName | UserPrincipalName | LastSignIn |
| Vendor User | vendor@external.com | 2025-09-15 |
| Consultant | consultant@gmail.com | 2025-08-20 |
⚠️ Always export reports before removing accounts.
$InactiveGuests | Select-Object `
DisplayName,
UserPrincipalName,
@{Name="LastSignIn";Expression={$_.SignInActivity.LastSuccessfulSignInDateTime}} |
Export-Csv "Inactive_Guest_Users.csv" -NoTypeInformation
Write-Host "✅ Guest inactivity report exported successfully!"
This script removes inactive guest accounts.
⚠️ Use carefully.
foreach ($Guest in $InactiveGuests) {
Remove-MgUser -UserId $Guest.Id
Write-Host "Removed guest user:" $Guest.UserPrincipalName
}
This approach is safer for production environments.
$CriticalThreshold = (Get-Date).AddDays(-180)
$VeryInactiveGuests = $Guests | Where-Object {
$_.SignInActivity.LastSuccessfulSignInDateTime -lt $CriticalThreshold
}
foreach ($Guest in $VeryInactiveGuests) {
Remove-MgUser -UserId $Guest.Id
Write-Host "Removed highly inactive guest:" $Guest.UserPrincipalName
}
Best practice:
foreach ($Guest in $InactiveGuests) {
Update-MgUser `
-UserId $Guest.Id `
-BodyParameter @{
accountEnabled = $false
}
Write-Host "Disabled guest user:" $Guest.UserPrincipalName
}
Inactive guest accounts are attractive attack targets.
👉 Removing them strengthens security posture.
Organizations often require:
This script simplifies cleanup operations.
Consultants and external vendors often retain access after projects end.
👉 Prevent unnecessary exposure.
Large numbers of stale guest users:
powershell.exe -File "RemoveInactiveGuests.ps1"
Use Task Scheduler for automation.
Recommended workflow:
Track:
| Error | Cause | solution |
| Insufficient privileges | Insufficient privileges to complete the operation |
Connect-MgGraph -Scopes ` "User.ReadWrite.All", "Directory.ReadWrite.All" Admin consent is required. |
| signInActivity is empty |
|
Verify licensing and review guest invitation history. |
| User not found | Guest account already deleted. | Validate existence before removal. |
| Accidental deletion risk | Removing active external users mistakenly. |
|
| Scenario | Benefit |
| Security cleanup | Reduce attack surface |
| Vendor lifecycle management | Remove stale access |
| Governance reviews | Simplify audits |
| Entra hygiene | Clean directory environment |
Inactive guest users are one of the most overlooked risks in Microsoft 365 environments.
Using Graph PowerShell, you can:
👉 Regular guest cleanup should be a core part of every Microsoft 365 governance strategy.
Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.
Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.
© Your Site Name. All Rights Reserved. Design by HTML Codex