2

I am trying to write a powershell script to delete groups and users from a site/sub-site. I have a parent site site1 and it have three sub-sites siteA siteB siteC

All three sub-sites have some common groups among them. Inheritance for all three sub-sites are broken, means they all have unique permissions.

Now manually if I am trying to delete these groups or users from one site, they will be deleted from that particular site itself. But when I try deleting groups through powershell, its automatically getting deleted from all sub-sites where that group or user is present.

What I am not able to do is through powershell script delete a group for a particular sub-site.

Below is the script which I tried(I found it online), its working fine to delete a group for all sub-site:

function Remove-SPGroup {
[CmdletBinding()]
    Param(
    [Microsoft.SharePoint.PowerShell.SPWebPipeBind]$Web,
    [string]$Group
    )
$SPWeb = $Web.Read()
# Prompting code
$title = "Delete SharePoint Group"
$message = "Do you want to delete the SharePoint Group?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Deletes the SharePoint Group."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Cancels and keeps the SharePoint Group."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $Host.UI.PromptForChoice($title,$message,$options,0)
switch ($result)
{
    0 {"Deleting $($Group) Group."}
    1 {"Operation cancelled..."}
}
# End Prompting code
if ($result -eq 0){
    try{
        $SPWeb.SiteGroups.Remove($Group)
    }
    catch{
        Write-Output "We got problem: $PSItem"
    }
    $SPWeb.Dispose()
}
else {return}
}
2

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.

$group = $web.SiteGroups[groupName];
$web.RoleAssignments.Remove($group);
$web.Update();
  • I am facing one issue: If subsite contains groups and users, through your code I am able to delete groups but when I am trying to delete a user, its throwing me an error. Ex: Under SiteC, there are 3 users and 2 groups. with the help of your code I am able to delete 2 groups but when I am trying to delete 3 users, I am getting error. – salah9 Dec 6 '17 at 15:50

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.