SharePoint Online: Delete a SharePoint Group using PowerShell

Requirement: Delete a SharePoint Online Group using PowerShell.

How to Delete a SharePoint Online Group?

If you’re looking for a quick and easy way to delete a group in SharePoint Online, this article is for you. In just a few steps, you can remove a group from your site with the web browser. We’ll also take a look at how to delete a group in SharePoint Online using PowerShell!

Here is how to remove a user group in SharePoint Online:

  1. Login to your SharePoint Online site >> Click on Settings gear >> Click Site Settings.
  2. On the Site Settings page, click on “People and groups” under the “Users and Permissions” section.
  3. Click on the Group Name from the left navigation. You may need to click on “More” and then select the group
    sharepoint online powershell delete group
  4. On the group page, click on Settings >> Group Settings
  5. On the group settings page, scroll down and click on the “Delete” button and confirm the prompt, click OK to remove the group in SharePoint Online.
    sharepoint online delete group powershell

This deletes the user group in SharePoint Online. Once you have completed these steps, the SharePoint group will be deleted from the site, and its members will no longer have access to the resources associated with that group.

Have you ever needed to delete a group in SharePoint Online, but didn’t want to go through the hassle of navigating the web interface? Sure, let’s delete a SharePoint Online group using PowerShell!

Removing a group from a site also removes the users within that group from the site.

Delete SharePoint Online Group using Remove-SPOSiteGroup PowerShell cmdlet

The Remove-SPOSiteGroup cmdlet lets you delete an existing SharePoint group from the SharePoint Online site collection. It requires you to pass the URL of the site collection, and the name of the group to delete. E.g., To delete the security group that you created in the previous step, you could run the following line of PowerShell:

#Variables for Admin Center & Site Collection URL
$AdminCenterURL = "https://Crescent-admin.sharepoint.com/"
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"

#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL -Credential (Get-Credential)

#sharepoint online delete group powershell
Remove-SPOSiteGroup -Site "https://Crescent.sharepoint.com/sites/marketing" -Identity "Marketing Managers"

This Remove-SPOSiteGroup cmdlet removes a SharePoint Online group from a site collection.

SharePoint Online: Delete Group using PowerShell

Here is the PowerShell to delete a group in SharePoint Online using PowerShell:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Function to remove a group from site
Function Remove-SPOGroup([String]$SiteURL, [String]$GroupName)
{
    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
    
        #Get the Group by name
        $Group=$Ctx.web.SiteGroups.GetByName($GroupName)
        $Ctx.ExecuteQuery()

        #Remove group from the web
        $Ctx.web.SiteGroups.Remove($Group)

        #Execute the query
        $Ctx.ExecuteQuery()
        write-host -f Green "Group '$GroupName' Deleted Successfully!" $_.Exception.Message
    }
    Catch {
        write-host -f Red "Error Removing Group!" $_.Exception.Message
    }
}
#Config Parameters
$SiteURL="https://crescent.sharepoint.com/sites/projects"
$GroupName="Project Managers"

#Call the function - powershell commands to delete sharepoint online groups
Remove-SPOGroup -SiteURL $SiteURL -GroupName $GroupName

This PowerShell deletes the given group in SharePoint Online.

Delete SharePoint Online Group using PnP PowerShell

Let’s delete a SharePoint Online group using the PowerShell cmdlet Remove-PnPGroup.

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$GroupName ="Contributors"

#Get Credentials to connect
$Cred = Get-Credential

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
#Delete a Group from site
Remove-PnPGroup -Identity $GroupName -Force

By following the steps outlined in this article, you can quickly and easily delete a group in SharePoint Online, and ensure that its members can no longer access the resources associated with that group. This can be helpful if you need to delete a group created by mistake or if you no longer need the group and want to remove it from your site.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *