Files
Scripting/Powershell/PowerShell-Office365Admin/AzureAD/BulkRemoveGuestsWithUnacceptedInvites.ps1
DistractADD cfddd4fad2 Added Files
2021-07-06 13:16:46 +10:00

22 lines
785 B
PowerShell

# Find all guests with unaccepted invites over a specific time period and remove them
#
# How many days are we allowing for invites to be accepted? (This can be 0 for all)
$cutOffDays = 30
# Install the AzureAD module, uncomment if required
#Install-Module -Name AzureAD
# Import the AzureAD module and connect to Azure AD
Import-Module -Name AzureAD
Connect-AzureAD
# Get the cut off date
$cutOffDate = (Get-Date).AddDays(-$cutOffDays)
# Get a list of all guests with unaccepted invites older than the specificed timeframe
$unacceptedGuests = Get-AzureADUser -All $true | Where-Object { $_.UserType -eq 'Guest' -and $_.UserState -eq 'PendingAcceptance' -and $_.RefreshTokensValidFromDateTime -lt $cutOffDate }
# Remove the guest accounts
$unacceptedGuests | Remove-AzureADUser