Snippets

The Brand Agency Remove old guest accounts from Azure AD related to The Brand Agency, allowing the ability to share content again.

Created by Josh Heglund
# Connect to Azure AD
Connect-AzureAD

# Get all guest users with an email address associated with The Brand Agency / King Street
$Users = Get-AzureADUser -Filter "(UserType eq 'Guest')" -Top 10000 | Where-Object { 
    ($_.UserPrincipalName -like "*_thebrandagency.co#EXT#@*") -or 
    ($_.UserPrincipalName -like "*_brandagency.com.au#EXT#@*") -or
    ($_.UserPrincipalName -like "*_thebrandagency.co.nz#EXT#@*") -or
    ($_.UserPrincipalName -like "*_kingst.co#EXT#@*") -or
    ($_.UserPrincipalName -like "*_brandxp.co#EXT#@*") -or
    ($_.UserPrincipalName -like "*_queenst.co#EXT#@*")
}

# Display the list of users
$Users

Read-Host "Press Enter to continue with the removal of the $($Users.Count) guest accounts listed above or press Ctrl + C to exit"

foreach ($User in $Users) {
    Write-Host "Removing guest account - $($User.UserPrincipalName)"
    Remove-AzureADUser $User
}

Write-Host -ForegroundColor Green "Complete"

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.