locked
Deleting SharePoint permission from a subsite RRS feed

  • Question

  • Below method deletes the existing SharePoint groups on a site... this works fine  while deleting a site collection..

    but while deleting groups in subsite.. We could see that the script has deleted permission groups for the sites that were not part of the subsite.. could some one help me on this

    Function Delete-SPGroup($ssite){
     <#
      .Description
      This PowerShell function Deletes an existing SharePoint Groups on the given site URL
     .Example
      Delete-SPGroup "http://sharepoint.com/sites/operations" 
      This example deletes SharePoint group  at the given site
      .Inputs
      $SiteURL - Site  URL in which the groups to be deleted
     #> 
        #Get the RootWeb
        $web = Get-SPWeb $ssite 
        if($web){
            # Check if web has Unique permission - Root webs always uses Unique permission 
            if (($web.HasUniqueRoleAssignments) -and ($web.IsRootWeb -eq $false)){ 
                  foreach($roleAssignment in $web.RoleAssignments){ 
                             $group = $web.SiteGroups[$roleAssignment.Member.Name];
                             $web.SiteGroups.Remove($group)
                             $web.Update();
                             Write-Host "Group Deleted-" $group -ForegroundColor Green 
                 }#foreach    
            }#if
            else{Write-host "Site doesn't have any unique permissions " $web.URL -ForegroundColor Red }
            $web.Dispose()
        }#if
    }#function Delete-SPGroup

    Wednesday, July 8, 2020 2:45 PM

All replies

  • Hi AdithyaDuyala,

    SharePoint groups are managed at site collection level.

    If you have the role inheritance broken at the sub-site, you can remove the association for specific groups. The result will be that group not having permissions for that site and not being displayed on the Site Permission page. However, the group will be available at the site collection level.

    Modify the PowerShell script as below:

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    
    
    Function Delete-SPGroup($ssite)
    { 
        #Get the RootWeb
        $web = Get-SPWeb -Identity $ssite 
        if($web)
        {
            # Check if web has Unique permission - Root webs always uses Unique permission 
                 if (($web.HasUniqueRoleAssignments) -and ($web.IsRootWeb -eq $false))
                { 
                        for($i=$web.RoleAssignments.Count-1;$i -ge 0; $i--)
                        { 
                                    Write-Host($web.RoleAssignments[$i].Member.Name);
                                    $group = $web.SiteGroups[$web.RoleAssignments[$i].Member.Name];
                                   	$web.RoleAssignments.Remove($group);
    								$web.Update();
                                    Write-Host "Group Deleted-" $group -ForegroundColor Green 
                        }   
                }
        
        else
        {   Write-host "Site doesn't have any unique permissions " $web.URL -ForegroundColor Red
        
        
        }
            $web.Dispose()
    	}
    }
    
    
    $siteUrl="http://sp/sites/MyDev/sub1"
    Delete-SPGroup($siteUrl)
    

    The PowerShell will delete the permission groups at sub site but won't affect the groups in root site collection.

    Here is a similiar question in StackOverflow for your reference:

    Delete groups/users of sub-sites SharePoint Powershell

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Thursday, July 9, 2020 6:02 AM
  • Hi AdithyaDugyala,

    Would you please share some update on this question ? Did you solve it ?

     

    If my reply is helpful to this question, you could Mark as answer so that it could also help others in the forum who have the similiar question.

     

    Thanks

     

    Best Regards


    "SharePoint" forums will be migrating to a new home on Microsoft Q&A !
    We invite you to post new questions in the "SharePoint" forums' new home on Microsoft Q&A !

    Friday, July 17, 2020 9:09 AM
  • We have tested the newly updated script and mentioned below are our findings.

    Although the subsites are getting deleted, the permission groups associated with the subsite are not being deleted. There is also one instance where the subsite was not deleted but the permission groups for that subsite was deleted.

    We had uploaded around 9 subsites for deletion and out of these 8 subsites were deleted, but the permission groups for these sub sites are intact. The permission group was deleted for the 9<sup>th</sup> subsite where the site has not been deleted. Please refer to the attachment labelled “Site Details” for additional information.

    Based on this, the script is not performing as it is expected to at the moment.

    Tuesday, July 28, 2020 2:37 PM