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.
- Add-PSSnapin Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue
-
- ##########
-
- #Script: Below Script will create a new group ion sharepoint
- #Author: Gowtham Rajamanickam
- #Date :15-03-2017
-
- ##########
- function CreateGroup
- {
- param ($URL, $GroupName, $PermissionLevel, $GroupDescription)
-
- try
- {
- #Retrive the Web
- $web = Get-SPWeb -Identity $URL
-
- #Check if web Exists already
- if($web -ne $null)
- {
- #Check if Group already exists
- if ($web.SiteGroups[$GroupName] -ne $null)
- {
- write-Host "Group $GroupName Already exists !" -ForegroundColor Red
- }
- else
- {
-
- $Web.SiteGroups.Add($GroupName, $web.Site.Owner, $web.Site.Owner, $GroupDescription)
-
- $Group = $web.SiteGroups[$groupName]
-
- $roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)
-
- $roleDefinition = $web.Site.RootWeb.RoleDefinitions[$permissionLevel]
-
- $roleAssignment.RoleDefinitionBindings.Add($roleDefinition)
-
- $web.RoleAssignments.Add($roleAssignment)
-
- $web.Update()
-
- write-Host "Group: $GroupName created successfully!" -ForegroundColor Green
- }
-
- $web.Dispose()
- }
- }
- catch [System.Exception]
- {
- write-host $_.Exception.ToString() -ForegroundColor Red
- }
- }
-
- #Call the function
- 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.