SharePoint 2013 Create A Group Using PowerShell

We have to understand the minimum required permissions. Prior to this, run Windows PowerShell for SharePoint cmdlet.

The users in the Farm administrators group or being the Farm Administrator to the SharePoint farm is not sufficient permission to run SharePoint cmdlets.

If you don't have the required permissions, you might receive the the error message- "The local farm is not accessible."

Load SharePoint assembly given below.
  1. Add-PSSnapin Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue  
  2.   
  3. ##########  
  4.  
  5. #Script: Below Script will create a new group ion sharepoint   
  6. #Author: Gowtham Rajamanickam  
  7. #Date  :15-03-2017  
  8.  
  9. ##########  
  10. function CreateGroup  
  11. {   
  12.     param ($URL, $GroupName, $PermissionLevel, $GroupDescription)   
  13.    
  14.     try  
  15.     {  
  16.         #Retrive  the Web  
  17.         $web = Get-SPWeb -Identity $URL  
  18.           
  19.         #Check if web Exists already   
  20.         if($web -ne $null)  
  21.         {  
  22.             #Check if Group already exists  
  23.             if ($web.SiteGroups[$GroupName] -ne $null)   
  24.             {   
  25.                 write-Host "Group $GroupName Already exists  !" -ForegroundColor Red  
  26.             }   
  27.             else   
  28.             {   
  29.                  
  30.                 $Web.SiteGroups.Add($GroupName, $web.Site.Owner, $web.Site.Owner, $GroupDescription)   
  31.                   
  32.                 $Group = $web.SiteGroups[$groupName]   
  33.                   
  34.                 $roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)   
  35.                   
  36.                 $roleDefinition = $web.Site.RootWeb.RoleDefinitions[$permissionLevel]   
  37.                   
  38.                 $roleAssignment.RoleDefinitionBindings.Add($roleDefinition)   
  39.                   
  40.                 $web.RoleAssignments.Add($roleAssignment)   
  41.                   
  42.                 $web.Update()   
  43.    
  44.                 write-Host "Group: $GroupName created successfully!" -ForegroundColor Green  
  45.             }   
  46.     
  47.             $web.Dispose()  
  48.         }  
  49.     }  
  50.     catch [System.Exception]  
  51.     {  
  52.         write-host $_.Exception.ToString() -ForegroundColor Red  
  53.     }  
  54. }  
  55.   
  56. #Call the function  
  57. CreateGroup "http://gowthamr.sharepoint.com" "TutorialReaders" "Read" "Tutorial Group for Readers"  
Reference
Thanks for reading my blog. Please post your feedback, so that I can improve my future articles and blogs.