1

I need to remove all permissions to a SharePoint 2007 site collection. It is a huge site with hundreds of sub-sites, lists and libraries that all have broken inheritance.

Is there a quick way to delete all permissions to the site?

2
  • Do you want to remove permission for all users of that site or for only one user ? Dec 9 '19 at 12:12
  • It will be for all users for that site apart from the Site Collection administrators.
    – SamB
    Dec 9 '19 at 13:29
1

I have altered the code to work in MOSS 2007, kindly try the below code.

    #region Variables 

[string]$WebUrl = "http://moss2007/" 
[string]$GroupNametoKeep = "Root Owners" 

#endregion 
#region Functions 

Function CleanUpAcl 
{ 
    param($SPObject, $GroupName) 

    $Title = $SPObject.Title 
    Write-Host "Removing permissions from $Title" 
    $RoleAssignmentsCount = $SPObject.RoleAssignments.Count 

    For($i = $RoleAssignmentsCount - 1; $i -ge 0; $i--) 
    { 
        if($SPObject.RoleAssignments[$i].Member.Name -ne $GroupName) 
        { 
            $DeletedGroupName = $SPObject.RoleAssignments[$i].Member.Name 
            Write-Host "`t Removing Group/User $DeletedGroupName..." 
            $SPObject.RoleAssignments.RemoveByID($SPObject.RoleAssignments[$i].Member.ID) 
        } 
    } 
} 

#endregion 
#region Main 

[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 
$Site = New-Object Microsoft.SharePoint.SPSite($WebUrl) 
$Web = $Site.OpenWeb() 
#$web.url 
$Lists = $Web.Lists
ForEach ($List in $Lists)
{ 
    if($List.HasUniqueRoleAssignments) 
    { 
        CleanUpAcl $List $GroupNametoKeep  
    }Else 
    { 
        $Response = Read-Host "List permissions are not unique, would you like to go to parent object with broken inheritance? (y/n)" 
        if(($Response -eq "y") -or ($Response -eq "yes")) 
        { 
            $BrokenSPObject = $List.FirstUniqueAncestor 
            CleanUpAcl $BrokenSPObject $GroupNametoKeep 
        } 
    } 
}

#Get All Users of the site collection 
$UserAccounts = @() 
foreach ($user in $web.SiteUsers) {
    $UserAccounts = $UserAccounts + $user.loginname 
}
  #Remove all users one by one. 
  foreach ($user in $UserAccounts) 
  { 
      try
       {
            #Set the Error Action 
            $ErrorActionPreference = "Stop" 
            #Remove User if not site admin 
            if(!$web.SiteUsers[$User].isSiteAdmin) 
            { 
                $web.SiteUsers.Remove($user) 
                Write-host "User Removed :" $user -ForegroundColor Green 
            } 
        } 
        catch {
                Write-host "Failed to remove the user:" + $user -ForegroundColor Red 
            } 
            Finally 
            { 
                #Reset the Error Action to Default 
                $ErrorActionPreference = "Continue" 
            } 
    }

#endregion 
3
  • Unfortunately this doesn't work with SP2007.
    – SamB
    Dec 12 '19 at 15:25
  • 1
    I have altered the code to work in MOSS 2007, kindly try the below code. Dec 13 '19 at 4:21
  • If my answer had helped you, kindly upvote and Mark it as answer, so that it will be helpful to others too Jan 25 '20 at 10:34
0

PowerShell is going to be your best bet. Here is a script to remove users from a security group. You can modify this to loop through all of your security groups in your site collection and clean them all out.:

$site = new-object Microsoft.SharePoint.SPSite("Site URL") 
$web = $site.OpenWeb() 
$oSiteGroup = $web.SiteGroups["Group Name"];
$oUsers = $oSiteGroup.Users
  foreach ($oUser in $oUsers) 
  { 
    "Removing user : " + $oUser.Name 
    $oSiteGroup.RemoveUser($oUser) 
  }

This site has some basic widely used PS scripts you can use to add to this script to help you get the users and get rid of them like you want. Use this script as a base, and you should be able to get it done.

3
  • Thanks. Will this also remove all those that are added individually as well as through groups?
    – SamB
    Dec 9 '19 at 13:30
  • No this is for Site security group.
    – lazoDev
    Dec 9 '19 at 14:02
  • See edit above...
    – lazoDev
    Dec 9 '19 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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