Forum Discussion
Remove bulk security groups using PowerShell
Hi All
I've created security groups in bulk using CSV.
I am trying to make a script to delete security groups from a CSV but not going anywhere:
Let's say I have security groups "NewGroup 1".."NewGroups 20" - 20 of them listen in a CSV
I am struggling to understand why I need to use -objectid and how can I add ForEach to remove the groups from the list only? or am I suppose to do it in a different way.
Any help would be appreciated.
Regards
Mahmoud
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.
6 Replies
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.
- Mahmoud ZiadaCopper Contributor
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.
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.
- What about using the former CSV to get the Groups you want to delete and actually delete it?
- Mahmoud ZiadaCopper Contributor
That's exactly what I am trying to do.
I want to be able to edit the csv file, adjust it to what needs to be deleted (that will be in hundreds probably) then run the script.
Any ideas?