How To Create SharePoint 2013 Custom Group Using Powershell Commands

Steps

Start your Windows PowerShell on your computer.

Right click and select run as an administrator option.

Paste the script given below on PowerShell Window and click enter button.

Check that your SharePoint site feature will be activated successfully.

Syntax Explanation

Add-SPGroup -webUrl "http://gowtham.sharepoint.com"
-groupName "SharePointOwner"
-description "SPOwners"
-ownerName "spdev\gowtham"
-defaultMemberName "spdev\gowtham" `\
-membership "spdev\gowtham"
  1. function Add-SPGroup {  
  2.   
  3.       
  4. [CmdletBinding()]  
  5. Param(  
  6.     [Parameter(Mandatory=$true)][string]$webUrl,  
  7.     [Parameter(Mandatory=$true)][string]$groupName,  
  8.     [Parameter(Mandatory=$true)][string]$description,  
  9.     [Parameter(Mandatory=$true)][string]$ownerName,  
  10.     [Parameter(Mandatory=$true)][string]$defaultMemberName,  
  11.     [Parameter(Mandatory=$true)][string]$membership  
  12. )  
  13. try {  
  14.      
  15.     Write-Host "Checking  Group already exists"  
  16.     $web = Get-SPWeb $webUrl;  
  17.     $spGroup = $web.SiteGroups[$groupName];  
  18.   
  19.     if ($spGroup -ne $null) {  
  20.         Write-Warning "Group $groupName already exists in this site!";    
  21.         return $spGroup;   
  22.     }  
  23.       
  24.     $owner = $web.EnsureUser($ownerName);  
  25.     $member = $web.EnsureUser($defaultMemberName);  
  26.   
  27.      Write-Host "Creating Group"  
  28.     $web.SiteGroups.Add($groupName, $owner, $member, $description);  
  29.     $spGroup = $web.SiteGroups[$groupName];  
  30.   
  31.    Write-Host "Adding users to Group"  
  32.     $usersToAdd = $membership.split(",");  
  33.     foreach($user in $usersToAdd) {  
  34.         $ensuredUser = $web.EnsureUser($user.trim());  
  35.         $spGroup.AddUser($ensuredUser);  
  36.     }  
  37.   
  38.     $web.RoleAssignments.Add($spGroup);  
  39.     $web.Dispose();  
  40.     return $spGroup;  
  41. }  
  42. catch {  
  43.    
  44.     Write-Error $Error[0];  
  45. } }  
Thanks for reading the blog.
Ebook Download
View all
Learn
View all