In this example, I am going to delete all Azure AD groups from my Azure AD tenant that have the word “test” in the name. This will be a fairly simple job to complete.
Full Script
Get-AzureADGroup -SearchString "test" | Export-CSV C:\temp\groups.csv
$groups = import-csv C:\temp\groups.csv
foreach ($group in $groups) {
$objectID = $group.objectID
Remove-AzureADGroup -ObjectID $ObjectID
}
Let’s break it down
Line 1 – Firstly we gather all Azure AD Groups with the string “test” in the name, then export this information to a CSV file in a temp for on C:\
Line 3 – We then import this CSV data into a variable called $groups so it can be manipulated in our PowerShell session.
Line 5>8 – This last section will loop through each group in our $groups array, select the ObjectID of that group and parse that information into our remote-AzureADGroup command.
You can verify the command has worked with the following:
Get-AzureADGroup -SearchString "test"