How to Create/Delete SharePoint Groups Programmatically with PowerShell?

Requirement: Create or Delete SharePoint User Groups using PowerShell

create delete sharepoint user group using powershell

These PowerShell code snippets helps to Create or Delete SharePoint Groups programmatically using PowerShell:

Create SharePoint Group using PowerShell:

Use this function to create a SharePoint group using PowerShell.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function Create-SPGroup($SiteURL, $GroupName, $GroupOwner,$GroupDescription)
{
 <#
  .Description
  This PowerShell function Creates new SharePoint Group  on the given site collection URL
 .Example
  Create-SPGroup "https://sharepoint.crescent.com/sites/operations" "Operation Managers" "Global\salaudeen" "Group for Operation Managers"
  This example Creates new SharePoint group "Operation Managers" at the given site collection 
 .Link
  https://www.sharepointdiary.com
 .Inputs
  $SiteURL - Site collection URL in which the group to be created
  $GroupName - Name of the Group to be create
  $GroupOwner - Existing user account, By default only Group owner can edit members of the group
  $GroupDescription - Descriptive text for the group
 .Outputs
  Gives message on the screen on Group Creation/Failure
 #>
 
 #Get the RootWeb
 $web = Get-SPWeb $SiteURL

 #Get Group Owner
 $GroupOwner = Get-SPUser -Identity $GroupOwner -Web $web

 #if the group doesn't exists already
  if($Web.SiteGroups[$GroupName] -eq $null)
   {
   #create sharepoint user group programmatically
   $web.SiteGroups.Add( $GroupName, $GroupOwner, $GroupOwner, $GroupDescription)

   #Get the Group
   $NewGroup = $Web.SiteGroups[$GroupName]

   #Set Group Properties
   #$NewGroup.AllowMembersEditMembership = $true
    
   #Assign Permissions to the Group
   $GroupAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($NewGroup)

   #Get Full Control Role (Permission Level)
   $GroupRoleDefinition = $web.Site.RootWeb.RoleDefinitions["Full Control"]

   #Bind Full Control
   $GroupAssignment.RoleDefinitionBindings.Add($GroupRoleDefinition)

   #Grant Full control to the Web
   $web.RoleAssignments.Add($GroupAssignment)
   $web.Update()
   
   Write-Host "New Group Created!"
  }
  else
  {
   Write-Host "Group already Exists!"
  }
 $web.Dispose()
 }

#Call the function to create a SharePoint user group programmatically
Create-SPGroup "https://sharepoint.crescent.com/sites/operations" "Operation Managers" "Global\Salaudeen" "Group for Operation Managers"

Delete SharePoint Group Programmatically with PowerShell:

And to delete an existing SharePoint group programmatically, the PowerShell code goes like this:

Function Delete-SPGroup($SiteURL, $GroupName)
{
 <#
  .Description
  This PowerShell function Deletes an existing SharePoint Group on the given site collection URL
 .Example
  Delete-SPGroup "https://sharepoint.crescent.com/sites/operations" "Operation Managers"
  This example deletes SharePoint group "Operation Managers" at the given site collection 
 .Link
  https://www.sharepointdiary.com
 .Inputs
  $SiteURL - Site collection URL in which the group to be created
  $GroupName - Name of the Group to be create
 .Outputs
  Gives message on the screen on Group Deletion/Failure
 #>
 
 #Get the RootWeb
 $web = Get-SPWeb $SiteURL

 #if the group exists 
  if($Web.SiteGroups[$GroupName] -ne $null)
   {
          #remove sharepoint group using powershell 
          $web.SiteGroups.Remove($GroupName)
          $web.Update()
   
          Write-Host "Group Deleted!"
  }
  else
  {
   Write-Host "Group doesn't Exist!"
  }
 $web.Dispose()
 }

#Call the function to delete sharepoint groups using powershell
Delete-SPGroup "https://sharepoint.crescent.com/sites/operations" "Operation Managers"

To add users to SharePoint group programmatically, refer to my another post: How to Add User To SharePoint Site Group Programmatically?

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

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