You can iterate through folders and set permissions with this script. It will go through all of your folders and set the permissions to contribute. Just set your site url, the list name and the group name/user name in the respective script.
How to grant permissions on group
Please check on test list before using in your productive environment.
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
$site = new-object Microsoft.SharePoint.SPSite("https://siteurl")
$web = $site.OpenWeb()
function GrantGroupPermission($groupName)
{
[Microsoft.SharePoint.SPGroupCollection]$spgroups = $web.SiteGroups
[Microsoft.SharePoint.SPGroup]$spgroup = $spgroups[$groupName]
Write-Host $spgroup.Name -ForegroundColor Red
$sproleass=new-object Microsoft.SharePoint.SPRoleAssignment([Microsoft.SharePoint.SPPrincipal]$spgroup)
$folder.BreakRoleInheritance("true")
$sproleass.RoleDefinitionBindings.Add($web.RoleDefinitions["Contribute"])
$folder.RoleAssignments.Add($sproleass);
Write-Host "Permission provided for group ", $groupName
}
$doclib=[Microsoft.SharePoint.SPDocumentLibrary]$web.Lists["ListNameHere"]
$foldercoll=$doclib.Folders;
foreach($folder in $foldercoll)
{
Write-Host $folder.Name
GrantGroupPermission("GroupNameHere")
}
$web.Close()
$site.Dispose()
If this script helps please set this answer as an answer for your question. Thanks
How to grant permissions on user
Please test in test library first.
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
$site = new-object Microsoft.SharePoint.SPSite("https://siteurl")
$web = $site.OpenWeb()
function GrantUserPermission($domainUserName)
{
[Microsoft.SharePoint.SPUserCollection]$spusers= [Microsoft.SharePoint.SPUserCollection]$web.SiteUsers
[Microsoft.SharePoint.SPUser]$spuser=$spusers[$domainUserName]
$sproleass=new-object Microsoft.SharePoint.SPRoleAssignment([Microsoft.SharePoint.SPPrincipal]$spuser)
$folder.BreakRoleInheritance("true")
$sproleass.RoleDefinitionBindings.Add($web.RoleDefinitions["Contribute"])
$folder.RoleAssignments.Add($sproleass);
Write-Host "Permission for user ", $domainUserName
}
$doclib=[Microsoft.SharePoint.SPDocumentLibrary]$web.Lists["ListNameHere"]
$foldercoll=$doclib.Folders;
foreach($folder in $foldercoll)
{
Write-Host $folder.Name
GrantUserPermission("Domain\domainUserNameHere")
}
$web.Close()
$site.Dispose()
### proof of concept group permission ###
Created a document library named "doc" on a test site.
Created two folders "a1" and "a2" with inheriting permissions from site in "doc".
Created two folders in each of the two folders. In "a1" there are two folders "a11" and "a12". In folder "a2" two folders were created as well - "a21" and "a22".
Structure of folders in "doc":
a1
-a11
-a12
a2
-a21
-a22
All folders have permission inherritation activated from site.
Now script was run as shown above with site URL and "doc" library name set. The group to add contribute is "Testseite TI Owners".
Console output of the script:
a1
Testseite TI Owners
Permission provided for group Testseite TI Owners
a2
Testseite TI Owners
Permission provided for group Testseite TI Owners
a11
Testseite TI Owners
Permission provided for group Testseite TI Owners
a12
Testseite TI Owners
Permission provided for group Testseite TI Owners
a21
Testseite TI Owners
Permission provided for group Testseite TI Owners
a22
Testseite TI Owners
Permission provided for group Testseite TI Owners
Checked permissions of the folders. All folders have broken inheritance with full set of permissions with "contribute" added to the Owners group.
Now cleaned up the permissions on a sub folder and removed everything except the Owners contribute.
Re-run the script with the "Testseite TI Members" group to check what happens when inheritance was broken before running the script.
Outcome as expected
a1
Testseite TI Members
Permission provided for group Testseite TI Members
a2
Testseite TI Members
Permission provided for group Testseite TI Members
a11
Testseite TI Members
Permission provided for group Testseite TI Members
a12
Testseite TI Members
Permission provided for group Testseite TI Members
a21
Testseite TI Members
Permission provided for group Testseite TI Members
a22
Testseite TI Members
Permission provided for group Testseite TI Members
Checked the folder permissions of a sub directory. Now two groups have permission.
Testseite TI Owners Contribute
Testseite TI Members Contribute
The script works fine to set permissions to directory structures to top folders as well as for sub folders no matter if permission inheritance was broken or was activated before running the script.
Tested on SharePoint 2016 on premise with April 2018 patch level on June 28 2018