Looking for a help in getting a PowerShell script to remove SharePoint subsite permissions alone. The groups should exist and users under the groups also. The script should only remove the permission levels assigned to the groups and the users directly added. We need to remove permissions of subsites from different site collections.
Can someone help me with this script please?
Thanks

Jignesh KumarRank #91 · 22.9K reputationPosted Feb 20, 2023, 2:15 AM EST
Hello,
Can you please try below,
Connect-PnPOnline -Url "https://your-site-url.sharepoint.com/sites/your-site" # Get the subsite $subsite = Get-PnPWeb -Identity "Subsite URL" # Get all the permission levels of the subsite $permissions = Get-PnPProperty -ClientObject $subsite -Property RoleAssignments.Include(RoleDefinitionBindings) # Remove the permission levels foreach ($permission in $permissions) { Remove-PnPRoleAssignment -Identity $permission.Id }Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 20, 2023, 2:25 AM EST
Hello Madhu,
Try below script.
# Set the subsite URL and the group/user name whose permissions need to be removed $subsiteUrl = "http:///" $groupName = "Group Name or User Name" # Get the subsite object $web = Get-SPWeb $subsiteUrl # Get the group/user object $group = $web.SiteGroups[$groupName] # Remove the group/user permissions from the subsite $web.RoleAssignments.Remove($group) # Dispose the objects $group.Dispose() $web.Dispose() # Remove group/user permissions from subsites in other site collections $siteCollections = @( "http://", "http://", "http://" ) foreach ($siteCollectionUrl in $siteCollections) { # Get the site collection object $site = Get-SPSite $siteCollectionUrl # Get the web application object $webApp = $site.WebApplication # Loop through all the subsites and remove the group/user permissions foreach ($web in $webApp.Sites | Select -ExpandProperty AllWebs) { if ($web.Url -ne $subsiteUrl) { # Check if the group/user has direct permissions in the subsite $roleAssignment = $web.RoleAssignments.GetAssignmentByPrincipal($group) if ($roleAssignment -ne $null) { # Remove the group/user permissions from the subsite $web.RoleAssignments.Remove($group) } } # Dispose the subsite object $web.Dispose() } # Dispose the objects $webApp.Dispose() $site.Dispose() }Thaks
Naimish Makwana
MadhuRank #5,790 · 286 reputationPosted Feb 20, 2023, 12:15 PM EST
Thank you Naimish and Jignesh for your quick response.
Hi Naimish, Need to add site URL, group name in the script you have shared. But we have over 500 sites which are from different site collections and we are not sure on how many groups those subsites and the permission levels too. We do not want to remove groups or users from the sites.
Hi Jignesh, Does your script will impact only the subsite that is dealing with or will it impact the sub-subsites also under the parent subsite. how to ignore sub-subsites, if it impacts all the sub-subsites under a parent susite?