SharePoint Online: Delete Group using PowerShell

Requirement: SharePoint Online Delete Group using PowerShell

How to Delete a SharePoint Online Group?
Here is how to remove a user group in SharePoint Online
  • Login to your SharePoint Online site >> Click on Settings gear >> Click Site Settings.
  • On the Site Settings page, Click on "People and groups" under Users and Permissions section.
  • 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
  • On the group page, click on Settings >> Group Settings
  • 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. Now, lets delete 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, 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://crescenttech-admin.sharepoint.com/"
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"

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

#sharepoint online delete group powershell
Remove-SPOSiteGroup -Site "https://crescenttech.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 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
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 SharePoint Online group using PowerShell
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"
$GroupName ="Contributors"

#Get Credentials to connect
$Cred = Get-Credential

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
#Delete Group
Remove-PnPGroup -Identity $GroupName -Force
SharePoint Online: Delete Group using PowerShell SharePoint Online: Delete Group using PowerShell Reviewed by Salaudeen Rajack on February 01, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.