Monday, August 5, 2013

How to Create/Delete SharePoint Groups Programmatically with PowerShell

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

Create SharePoint Group Programmatically with 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 "http://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
  http://www.sharepointdiary.com
  http://twitter.com/SharePointdiary
 .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 "http://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:
Function Delete-SPGroup($SiteURL, $GroupName)
{
 <#
  .Description
  This PowerShell function Deletes an existing SharePoint Group on the given site collection URL
 .Example
  Delete-SPGroup "http://sharepoint.crescent.com/sites/operations" "Operation Managers"
  This example deletes SharePoint group "Operation Managers" at the given site collection 
 .Link
  http://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 Exists!"
  }
 $web.Dispose()
 }

#Call the function to delete sharepoint groups using powershell
Delete-SPGroup "http://sharepoint.crescent.com/sites/operations" "Operation Managers"
To add users to SharePoint group programmatically, refer my another post: How to Add User To SharePoint Site Group Programmatically



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...