question

LupsaRafila-0471 avatar image
0 Votes"
LupsaRafila-0471 asked EmilyDu-MSFT commented

SHP 2013 remove group

Hi,

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

Thank you!

windows-server-powershelloffice-sharepoint-server-administration
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@LupsaRafila-0471

I'm checking how the things are going on about this issue. Whether the answer helps you?

You can accept the answer if it helps.


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 Votes 0 ·

1 Answer

EmilyDu-MSFT avatar image
0 Votes"
EmilyDu-MSFT answered EmilyDu-MSFT edited

@LupsaRafila-0471

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.



1.png (15.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.