How to Delete a SharePoint 2013 Multiple Groups Using Powershell

Introduction

This article explains how to delete a SP2013 multiple group in Excel using a Powershell script.

Prerequisites

  1. Ensure you have created a site collection and groups in a site collection.
  2. Ensure you have a Powershell window to execute the script.

Use the following procedure

  1. In this article will be explaining how to export a group to a CSV file and then delete the group that you don't want in the site using Powershell.
  2. Please refer to my article if you want to learn how to export the groups in Excel using Powershell:

     Export SharePoint 2013 Site Collection Groups Name in Excel Using Powershell.
     
  3. Create a group by the name MyGroup in your site collection.
  4. Once your are done with your export, open the CSV file; you will be able to see the list of groups that are available in the site collection in this CSV file.
  5. Now in order to delete a specific group, we will create one more column with the name “RemoveGroup” in the ExportGroups.csv file and set the value to either Yes/ No.
  6. If the value is set to "Yes" then we will delete only that group.
  7. In this example we will delete a group that we created recently, "MyGroup".

    delete a group

  8. And let's set the value of RemoveGroup to “Yes” only for “MyGroup”.
  9. Suppose you want to delete another group as well; then you can mark the RemoveGroup value to yes againt your group name.Thus you can delete multiple groups in one shot.
  10. Add the following lines of code to a text editor such as Notepad.
  11. Save the file as “RemoveGroups.ps1”.
     

    #get the site object

    $site = Get-SPSite “ http:// yoursitecollection”

    #get the group collection details

    $groups = $site.RootWeb.sitegroups

    #set the location where you have placed the CSV file

    $inputCSV ="C:\ ExportGroups.csv"

    #import the csv file to dbrows

    $dbRows = IMPORT-CSV $inputCSV

    Write-Host "Count " $dbRows.Count

    write-host -ForegroundColor Green "Starting for Deletion of a group Operation"

     

    #for each of the row in csv file check if the RemoveGroup value is set to Yes.

    FOREACH ($dbRow in $dbRows)

    {

        write-host -ForegroundColor Green "Processing " $dbRow.GroupName

        if($dbrow.RemoveGroup -eq "Yes")

        {

            write-host "Deleting the group :" $dbRow.GroupName

            #delete the group

            $groups.Remove($dbRow.GroupName)

        }

        else

        {

            write-host -ForegroundColor yellow "Skipping for " $dbRow.GroupName

        }

    }
     

  12. That's it!! Now let's start testing.

Testing

  1. Now to run the script, open the Powershell window by clicking on Run as administrator.
  2. Now navigate to the location that you have placed your Powershell script; in our case it's “RemoveGroups.ps1” at the location (cd “C:\RemoveGroups.ps1”).
  3. Click on enter button.Thats it.
  4. Now if you navigate to your site collection then you will not be able to see the “MyGroup” group in your site collection.

Summary

Thus in this article you saw how to delete a SP2013 multiple group in Excel using a Powershell script.