Forum Discussion
Remove bulk security groups using PowerShell
- Jan 10, 2018
Without sharing the CSV file we can only guess. For example something like this should work:
Import-Csv blabla.csv | % { Get-MsolGroup -SearchString $_.Name | Remove-MsolGroup}
That's assuming you have a field NAME in the CSV file, designating the groups. It *must* be unique, otherwise you might end up deleting a bunch of other groups. And there is no recover delete group option, so use with care.
Well the Remove- cmdlet only works with ObjectId, so you don't have much choice. If you want to use any other parameters to filter them out, you have to run Get-MsolGroup first:
Get-MsolGroup | ? {$_.DisplayName -eq "test"}
You can then either get the ObjectId or simply pipe to Remove-MsolGroup.
I was playing with those two lines yesterday, but how can I use the first one with the csv file? I don't want to search criteria, I need it to be from the list have in a csv for example.
- VasilMichevJan 10, 2018MVP
Without sharing the CSV file we can only guess. For example something like this should work:
Import-Csv blabla.csv | % { Get-MsolGroup -SearchString $_.Name | Remove-MsolGroup}
That's assuming you have a field NAME in the CSV file, designating the groups. It *must* be unique, otherwise you might end up deleting a bunch of other groups. And there is no recover delete group option, so use with care.
- Mahmoud ZiadaJan 15, 2018Copper Contributor
Thank you that was helpful.
It seems this also works:
Import-CSV c:\groups.csv | Remove-MsolGroup –Force