SharePoint: Create site Group with Permission Using PowerShell


 cls
 Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
 Add-PSSnapin Microsoft.SharePoint.Powershell

 function ADDGroup([string]$GroupName,[string]$OwnerName,[string]$MemberName,[string]$Description, [string]$SiteUrl,[string]$PermissionLevel) {
  $Site= Get-SPSite -Identity $SiteUrl
 $SPWeb = $Site.RootWeb

 if ($SPWeb.SiteGroups[$GroupName] -ne $null){
  Write-Host "Group "$GroupName" already exists!"
  Break;
 } else
 {
  $owner = $SPWeb | Get-SPUser $OwnerName
  if ($MemberName -ne "") {  $member = $SPWeb | Get-SPUser $MemberName }
  $SPWeb.SiteGroups.Add($GroupName, $owner, $member, $Description)
  $SPGroup = $SPWeb.SiteGroups[$GroupName]
  $roleDefinition = $SPWeb.Site.RootWeb.RoleDefinitions[$PermissionLevel]
  $roleAssigment = new-object Microsoft.SharePoint.SPRoleAssignment($SPGroup)
  $roleAssigment.RoleDefinitionBindings.Add($roleDefinition)
  $SPWeb.RoleAssignments.Add($roleAssigment)
  $SPWeb.Update()
 }

  $SPWeb.Dispose()
  return $ErrMessage
}

ADDGroup "GroupName" "Owner" "Member" "Description" "http://sp2010:8080/personal/Test2/" "PermissionLevel"


Comments

Popular posts from this blog

C# Copy files from one server to another

Export SharePoint Online Group Users to Excel using Powershell