Delete security groups from Microsoft 365 admin center which one synced from on-premise AD

Szilágyi Sándor 51 Reputation points
2022-05-19T08:50:44.903+00:00

In the past i synced all users and groups from my on-prem AD. I delete all of them yet however i found security groups whichone doesn't delete yet. I want to delete them now because i don't need them.

I list of them with that command:

Get-AzureADGroup

I try delete with that command:

$groupName="ADgroupName"
Remove-AzureADGroup -ObjectId (Get-AzureADGroup | Where { $_.DisplayName -eq $groupName }).ObjectId

There are nearly 40 groups, so deleting one by one would be a rather slow solution. Does have any scripts for ther or etc?

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,893 questions
0 comments No comments
{count} vote

Accepted answer
  1. !Daniel Bradley 1,056 Reputation points MVP
    2022-05-19T09:51:26.403+00:00

    Hi @Szilágyi Sándor !

    I wrote a short script that I tested on my tenant.

    Firstly gather the groups you wish to delete into a CSV file. I have used the SearchString parameter to find all groups with 'Test' in the name.

    Get-AzureADGroup -SearchString "test" | Export-CSV C:\temp\groups.csv  
    

    Next, import your CSV data into a variable so we can manipulate in our PowerShell session:

    $groups = import-csv C:\temp\groups.csv  
    

    Lastly we will look through our groups, save the ObjectID and run that against the remove command:

       foreach ($group in $groups) {  
          $objectID = $group.objectID  
          Remove-AzureADGroup -ObjectID $ObjectID  
        }  
    

    If the answer is helpful please click on ACCEPT ANSWER as it could help other members of the Microsoft Q&A community who have similar questions and are looking for solutions.


1 additional answer

Sort by: Most helpful
  1. Edmund Fordjour 0 Reputation points
    2024-05-14T15:50:44.3566667+00:00

    Your scripts are helpful. I modified them a little bit to suite my case. Thank you

    0 comments No comments