PowerShell script to delete Azure guest accounts with "UserState" in "pendingAcceptance" for over 6 months

I don’t have access to an Azure Tenant at the moment … so it is untested …

Get-AzureAdUser -All $true -Filter "usertype eq 'guest'" | 
Where-Object -Property Userstate -EQ -Value 'PendingAcceptance' | 
ForEach-Object {
    $ADUserExtension = Get-AzureADUserExtension -ObjectId $_.ObjectId

    [PSCustomObject]@{
        UserPrincipalName = $_.UserPrincipalName
        UserState         = $_.UserState
        UserType          = $_.UserType
        CreationType      = $_.CreationType
        Mail              = $_.Mail
        CreationDate      = $ADUserExtension.CreationDate
    }
} | 
Export-Csv -Path C:\Temp\DeletedGUsers6.csv -NoTypeInformation
1 Like