How to assign permission level to SharePoint 2013 groups using PowerShell?


In this post, we will discuss how to assign permission levels to SharePoint 2013 groups. In my previous post we have discussed how to create groups in site collection using PowerShell. Below I will be sharing code to provide security permission to different SharePoint Groups. The code is applicable for SharePoint 2010 and SharePoint 2013.

Create CSV file named as ChangeGroupPermAtWebLevel.CSV and add below columns.

WebURL
WebHasUniquePermission
GroupName
ChangeToPermissionLevel

You can provide any name to CSV but change the file name details in PS script code.


How to assign permission level to SharePoint 2013 groups using PowerShell?
How to assign permission level to SharePoint 2013 groups using PowerShell?

PowerShell Script:

$0 = $MyInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
$path = $(“$dp0\ChangeGroupPermAtWebLevel.csv”)
$csv = Import-csv -path $path

foreach($line in $csv)
{
$siteURL = $line.WebURL
$groupName = $line.GroupName
$PermLevel = $line.ChangeToPermissionLevel
$CurrentPermLevels = $line.GroupPermission

write-host $siteURL “….” $groupName “……” $PermLevel

if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PSSnapin Microsoft.SharePoint.PowerShell
}

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”) | Out-null

$web = Get-SPWeb $siteURL

if ($web.HasUniquePerm -eq $true)
{
$group = $web.SiteGroups[$groupName]
if($group.Roles.Count -eq “0”)
{
$assign = New-Object Microsoft.SharePoint.SPRoleAssignment($group);
#$objDefination=$web.RoleDefinitions.GetByName($PermLevel);
$assign.RoleDefinitionBindings.Add($web.RoleDefinitions[$PermLevel])
$web.RoleAssignments.Add($assign);
$web.Properties.Update()
}
else
{
$assign = $web.RoleAssignments.GetAssignmentByPrincipal($group);

for ($i = $assign.RoleDefinitionBindings.Count – 1; $i -ge 0; $i–-)
{
$assign.RoleDefinitionBindings.Remove($i)
$assign.Update()
}
$assign.Update()
$assign.RoleDefinitionBindings.Add($web.RoleDefinitions[$PermLevel])
$assign.Update()
}
Write-Host $PermLevel “Permission Level Assigned….”
}
$web.Dispose
}

Read some SharePoint csom articles:


Thanks,
Sambita

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Sambita Rath

I have 9 years of exprience into SharePoint implementation, architecture, Administrator, development and Training.Designing Information Architecture in SharePoint 2007, 2010,2013 and Office 365.

View all posts by Sambita Rath →