Forum Discussion

Mahmoud Ziada's avatar
Mahmoud Ziada
Copper Contributor
Jan 09, 2018
Solved

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

  • VasilMichev's avatar
    VasilMichev
    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.

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 Ziada's avatar
      Mahmoud Ziada
      Copper 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.

       

      • VasilMichev's avatar
        VasilMichev
        MVP

        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 Ziada's avatar
      Mahmoud Ziada
      Copper 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?

Resources