Skip to content

Instantly share code, notes, and snippets.

@ppartarr
Created July 15, 2020 21:25
Show Gist options
  • Save ppartarr/66d4400f3ee29eed95eb1a03f544ac19 to your computer and use it in GitHub Desktop.
Save ppartarr/66d4400f3ee29eed95eb1a03f544ac19 to your computer and use it in GitHub Desktop.
Remove all Azure AD groups starting with a given prefix
# This script requires :
# 1. the Azure AD Powershell module: Install-Module -Name AzureADPreview
# 2. an authenticated user: Connect-AzureAD
$uid = ""
$groupPrefix = "GroupNamePrefix"
$groups = Get-AzureADMSGroup
$groups | Select-Object -Skip 3 | Where-Object {
$_ -match '(\d+)\s+(\d+)\s+(.*?)\s+([a-z]+(?:-[a-z0-9]+){2,})\s+(.*)'
} | ForEach-Object {
New-Object -Type PSObject -Property @{
'Id' = $matches[0]
'DisplayName' = $matches[1]
'Description' = $matches[2]
}
}
forEach($group in $groups){
if ($group.DisplayName.StartsWith($groupPrefix)) {
# remove group
Remove-AzureADGroup -ObjectId $group.Id
Write-Output "removed group: $($group.DisplayName) $($group.Id)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment