SHP 2013 remove group

Lupsa Rafila 1 Reputation point
2021-03-19T11:44:21.31+00:00

Hi,

Can someone help me with a powershell script that will remove a specific group from multiple site collections ?

Thank you!

SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,798 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Emily Du-MSFT 41,786 Reputation points Microsoft Vendor
    2021-03-22T08:52:36.75+00:00

    @Lupsa Rafila

    1.For a specific SharePoint group, please run below PowerShell.

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue  
    
    $GroupName = "group name"  
    
    $SiteColl = Get-SPWebApplication  "web application URL" | Get-SPSite -Limit All | Get-SPWeb -Limit All  
    
    ForEach($_ in $SiteColl) {  
    If($_.SiteGroups[$GroupName] -ne $null)  
    {  
    $_.SiteGroups.Remove($GroupName)  
    }  
    }  
    

    2.For a specific AD group, please run below PowerShell.

    Note: Go to site settings -> Site permission -> Select the AD group -> Find the AD group account.

    80137-1.png
    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

    Function Delete-UserFromAllSites([string]$WebAppURL, [string]$UserAccount, [bool]$ScanOnly)  
    {   
       $WebApp = Get-SPWebApplication $WebAppURL  
    
       foreach ($Site in $WebApp.Sites)  
       {  
        try  
          {  
          $ErrorActionPreference = "Stop"  
          $User = $Site.RootWeb.SiteUsers | Where-Object {$_.LoginName -eq $UserAccount}        
          if($User -ne $null)  
          {  
              $Site.RootWeb.SiteUsers.Remove($UserAccount)  
              Write-Host "User Deleted from: $($site.Rootweb.URL)"         
          }  
        }  
        catch  
               {   
                  #Write error message on screen and to a LOG file  
                  write-host "Error Deleting user from site collection: $($site.rootweb.url)"  
               }  
              finally  
              {  
                  $ErrorActionPreference = "Continue"  
                  $site.Dispose()  
              }  
       }  
    }  
    
    Delete-UserFromAllSites "web application URL" "AD group account" $true  
    

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments