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}
}