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. #Script: Below Script will create a new group ion sharepoint
  4. #Author: Gowtham Rajamanickam
  5. #Date :15-03-2017
  6. ##########
  7. function CreateGroup
  8. {
  9. param ($URL, $GroupName, $PermissionLevel, $GroupDescription)
  10. try
  11. {
  12. #Retrive the Web
  13. $web = Get-SPWeb -Identity $URL
  14. #Check if web Exists already
  15. if($web -ne $null)
  16. {
  17. #Check if Group already exists
  18. if ($web.SiteGroups[$GroupName] -ne $null)
  19. {
  20. write-Host "Group $GroupName Already exists !" -ForegroundColor Red
  21. }
  22. else
  23. {
  24. $Web.SiteGroups.Add($GroupName, $web.Site.Owner, $web.Site.Owner, $GroupDescription)
  25. $Group = $web.SiteGroups[$groupName]
  26. $roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)
  27. $roleDefinition = $web.Site.RootWeb.RoleDefinitions[$permissionLevel]
  28. $roleAssignment.RoleDefinitionBindings.Add($roleDefinition)
  29. $web.RoleAssignments.Add($roleAssignment)
  30. $web.Update()
  31. write-Host "Group: $GroupName created successfully!" -ForegroundColor Green
  32. }
  33. $web.Dispose()
  34. }
  35. }
  36. catch [System.Exception]
  37. {
  38. write-host $_.Exception.ToString() -ForegroundColor Red
  39. }
  40. }
  41. #Call the function
  42. 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.