How to run through all folders in a specific document library to remove inheritance and then remove all users from all folders within a specific Document Library using a Powershell script?

1 Answer 1


Below are couple of links i mentioned, 1st one using powershell to remove the user and groups from DL and 2nd one is using the code( you can try to create equal powershell for it)

For Powershell, read this.

For code check this:

Breaking permission inheritance

This code will give you how to break inheitance and remove user.

SPFolder folder = _MyWeb.GetFolder(documentLibrary + txtFolder.Text);
             SPGroupCollection spc = _MyWeb.SiteGroups;
             SPUserCollection _spUSer = _MyWeb.AllUsers;
      folder.Item.BreakRoleInheritance(true);//Break the inheritance of the created folder in the Document Library 

The following code will remove all users from the folder. It won't remove the current user who is doing this task.

 foreach (SPUser oUser in _spUSer)
  SPPrincipal principal = (SPPrincipal)oUser;
  SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)oUser);
        _MySite.AllowUnsafeUpdates = true;
      _MyWeb.AllowUnsafeUpdates = true;
   string ostrCurrentUserName = oUser.LoginName.ToString().Substrin
(oUser.LoginName.ToString().IndexOf('\\') + 1);

                                catch (Exception exx)
           lblMessage.Visible = true;

                                    lblMessage.Text = oUser + "Doesnt have permission to create

                            //Removing the  sharepoint groups from the folder
                            foreach (SPGroup oGroup in spc)
                                // Response.Write(SPEncode.HtmlEncode(oGroup.Name) + "<BR>");
                                SPRoleAssignment roleAssignment = newSPRoleAssignment((SPPrincipal)oGroup);

                                _MySite.AllowUnsafeUpdates = true;
                                _MyWeb.AllowUnsafeUpdates = true;


Ref:- How To make SharePoint document library folder unique Permission Programmatically

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.