Share via


SharePoint - Remove Folder Permissions using Powershell

Question

Monday, July 9, 2018 2:56 PM

Hi,

I am trying to pull together a script that will iterate through a library and remove and grant permissions on a folder with a specific name in a library.

I have the following script which works with granting permissions, but won't 'remove' permissions on the folder:

Add-PSSnapin Microsoft.SharePoint.PowerShell

# URL of Site
$PrivateGroup="DB Private"
$RemovalGroup1="Team-Test Members"
$PermissionLevel="Contribute"
$web = Get-SPWeb -Identity "http://teams-test.com/it/team-test"
$folderName = "ZZTest"

# Name of the list
$list = $web.Lists["Locations"]

$folders = $list.Folders
foreach($folder in $folders)
{
    if($folder.name -eq "ZZTest")
        {         
                    Write-Host $folder.name
         $folder.BreakRoleInheritance($true)
                     
                    #Get Group
                    $AddGroup = $Web.SiteGroups[$PrivateGroup]
                    $roleAssignment = new-object Microsoft.sharepoint.SPRoleAssignment($AddGroup)
                    $RemGroup = $web.SiteGroups[$RemovalGroup1]
                    $RoleRemoval = new-object Microsoft.sharepoint.SPRoleAssignment($RemGroup)
                                                                               
                    #Get Permission Level
                    $roleDefinition = $web.RoleDefinitions[$PermissionLevel]
                    $roleAssignment.RoleDefinitionBindings.add($roleDefinition);
                    $RoleRemoval.roledefinitionBindings.add($roleDefinition);                    
                                                            
                    #RemoveGroup                
                    $folder.RoleAssignments.Remove($RoleRemoval)
                    $folder.SystemUpdate();                                                                    

                    #Grant Access to Group
                    $folder.RoleAssignments.Add($roleAssignment)
                    $folder.SystemUpdate();     
            }
       }                 
$web.Dispose()
#

I get the following error on the "Remove"

Cannot find an overload for "Remove" and the argument count: "1".

At D:\Powershell\GetFolders.ps1:32 char:21

+ $folder.RoleAssignments.Remove($RoleRemoval)

+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : NotSpecified: (:) [], MethodException

    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

Any ideas or advice would be appreciated!

Thanks

All replies (5)

Monday, July 23, 2018 1:22 PM âś…Answered | 1 vote

Add-PSSnapin Microsoft.SharePoint.PowerShell

# URL of Site
$PrivateGroup="DB Private"
$RemovalGroup1="Team-Test Members"

$contributePermission="Contribute";
$editPermission = "Edit";

$web = Get-SPWeb -Identity "http://teams-test.com/it/team-test"
$folderName = "ZZTest"

# Name of the list
$list = $web.Lists["Locations"]

$folders = $list.Folders
foreach($folder in $folders)
{
    if($folder.name -eq "Private")
        {         
                    Write-Host $folder.name
                    $folder.BreakRoleInheritance($true)
                     
                    #Get Group
                    $AddGroup = $web.SiteGroups[$PrivateGroup]
                    $roleAssignment = new-object Microsoft.sharepoint.SPRoleAssignment($AddGroup)
                    $RemGroup = $web.SiteGroups[$RemovalGroup1]
                    $roleRemoval = new-object Microsoft.sharepoint.SPRoleAssignment($RemGroup)
                                                                               
                    #Get Permission Level
                    $roleDefinition = $web.RoleDefinitions[$contributePermission];
                    $editDefinition = $web.RoleDefinitions[$editPermission];

                    $roleAssignment.RoleDefinitionBindings.add($roleDefinition);
                    $roleRemoval.RoleDefinitionBindings.add($editDefinition);            
                                              
                    $group = $folder.RoleAssignments.Groups[$RemovalGroup1];
                                                                                
                    #RemoveGroup                
                    $folder.RoleAssignments.Remove($group)
                    $folder.SystemUpdate();                                                                    

                    #Grant Access to Group
                    $folder.RoleAssignments.Add($roleAssignment)
                    $folder.SystemUpdate();     
            }
       }                 
$web.Dispose()
#

I managed to get the above code working to Add group, and remove a group.


Monday, July 9, 2018 9:20 PM

Hi,

You can try the below

http://www.sharepointdiary.com/2016/09/sharepoint-online-set-folder-permissions-powershell.html

Please remember to click Mark as Answer on the answer if it helps you


Friday, July 13, 2018 9:37 AM

Hi,

 

How are you today? Is there any update or any other assistance I could provide on this issue?

 

If you think Wilsmatt's suggestion is helpful, you could mark it as an answer. It will help others who meet the similar question in this forum.

 

Best regards,

Ethan Liu

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].


Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.


Friday, July 13, 2018 1:09 PM

Hi Ethan,

Thanks for your reply, on reviewing the script it doesn't help my situation as I am trying to "Remove" permissions after "Granting Permission".

As per my original post, the "Grant Permission" step is working fine, it's the remove of the permissions I can't get to work?

Thanks

Matt


Monday, August 6, 2018 3:17 AM

Hi Wilsmatt,

As you have solved this issue, please remember to mark your reply as an answer, it will be beneficial to others in this forum who meet the same issue in the future.

Thanks,

Wendy

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].


Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.