Bulk user deletion from sharepoint sitecollection

SKIFIN JOSEPH 1 Reputation point
2022-08-18T10:45:32.683+00:00

Hi,
We are using two domains in our sharepoint foundation 2013 farm , is there any powershell command/script to delete only one of the particular domain users from our sitecollection ?

Microsoft 365 and Office | SharePoint Server | For business
{count} votes

1 answer

Sort by: Most helpful
  1. Jinwei Li-MSFT 4,736 Reputation points Microsoft External Staff
    2022-08-19T02:50:18.487+00:00

    Hi @SKIFIN JOSEPH ,

    Please try to use this:

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue  
       
    Function Delete-UserFromAllSites([string]$WebAppURL, [string]$UserID)  
    {  
        #Get All Site collections  
        $SitesColl = Get-SPWebApplication $WebAppURL | Get-SPSite -Limit All  
       
        foreach($Site in $SitesColl)  
        {  
            write-host "Processing site:" $Site.RootWeb.URL  
               
            #Check if user Exists in the site collection  
            $User = $Site.RootWeb.SiteUsers | Where-Object {$_.LoginName -eq $UserID}  
                  
            #If user account found  
            if($User -ne $null)  
            {  
                #Remove User from the Site  
                Remove-SPUser $UserID -web $Site.RootWeb.URL -confirm:$false  
                write-host "Removed user from site collection:"$Site.RootWeb.URL -f Green  
            }         
        }     
    }  
       
    #Variables for processing  
    $WebAppURL = "https://Portal.Crescent.com/"  
    $UserID="Crescent\DaveP"  
       
    #Call the function  
    Delete-UserFromAllSites $WebAppURL $UserID  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.