Delete Users from SharePoint Site Collection using PowerShell

Requirement: Delete user from site collection in SharePoint 2013 

How to delete a user from site collection?

So you don’t want a particular user to have access to SharePoint 2010 site anymore! Want to delete users from SharePoint 2010 site collection, isn’t it? Well, How to delete users from site collection?

  1. To delete a user in SharePoint 2010, Navigate to the site collection as a Site Collection Administrator
  2. Click on Site actions >> Site permissions
  3. Click on any SharePoint group. Now the URL will look something like this:
  4. Change the MemberShipGroupID parameter’s value from 11 to 0. i.e. :
    https://your-site-collection-url/_layouts/people.aspx?MembershipGroupID=0 and hit the “Enter” key. This will lead you to see the All People page.
  5. Select the user you want to delete, and click on Actions >> “Delete user from Site collection”
    sharepoint delete user from site collection powershell

That’s it!

By the way, to delete user in SharePoint 2007, “All People” page link is straightforward. Just navigate to: Site Actions >> Site Settings >> Advanced Permissions >> All People (In Quick Launch!)

Delete Users from SharePoint Site Collection using PowerShell

PowerShell comes really handy on occasions of repeated manual tasks. Let’s use PowerShell to delete users from SharePoint 2010 site collection.

PowerShell script in SharePoint 2010 to delete user from site collection

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Get the Root web
$web = Get-SPWeb ""
#Supply the user account 
$UserAccount = "Global\DaveP"
#Removes user from site collection
Remove-SPUser -Identity $UserAccount -Web $Web -Confirm:$False

This PowerShell script deletes a particular user from given site collection. Also it deletes user from userinfo table! Remember, SharePoint cannot delete user if the user is Site collection administrator!!

SharePoint delete user from site collection programmatically using C#:
We can also use C# object model code in SharePoint to delete user programmatically:

//Define the parameter values: Site collection URL and user account to remove
string siteURL = "";
string userAccount = @"Global\MarkM";

using (SPSite site = new SPSite(siteURL))
	using (SPWeb web = site.OpenWeb())
		//Get the User
		SPUser user=null;
			 user = web.SiteUsers[userAccount];  
		catch (SPException ex) {//user Not found in the site collection}
		//Check if the given user is valid and found in the site collection
		if (user !=null)
			//Remove the user if he is not a Site collection administrator
			if (!user.IsSiteAdmin)
				//Remove the user from site collection
				Console.WriteLine("User removed from:"+site.RootWeb.Url);

This removes user completely from SharePoint site collection.

Delete user from all site collections in SharePoint 2010 using PowerShell:

Sometimes, we may need to delete a particular user from all site collections. Say for e.g. Employee leaves the company! Let’s delete the user from site collection with PowerShell.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function Delete-UserFromAllSites([string]$WebAppURL, [string]$UserAccount, [bool]$ScanOnly)
   Function to Search and Delete given user from all site collections of the web application
   This PowerShell function Scans each site collection under all site collections of the given web application
   and deletes the user from site collections.
   DeleteUserFromAllSites "" "global\davep" $true
   This example removes the user account "global\davep" from the web application ""
   $WebAppURL - URL of the web application in which the user account to be scaned and or deleted
   $UserAccount - User account to delete from all sites
   $ScanOnly - Indicates whether the script should delete the user or only scan
   Writes the output on screen: List of site collections from where the user account is found/removed.

   #Get the web application
   $WebApp = Get-SPWebApplication $WebAppURL
   #Loop through each site collection
   foreach ($Site in $WebApp.Sites)
      $ErrorActionPreference = "Stop"
      #Try to get the User
      $User = $Site.RootWeb.SiteUsers | Where-Object {$_.LoginName -eq $UserAccount}
      #If user account found
      if($User -ne $null)
       if($ScanOnly -eq $true)
        Write-Host "Found user on: $($site.Rootweb.URL)"
          #Remove the User from site collection
          Write-Host "User Deleted from: $($site.Rootweb.URL)"
              #Write error message on screen and to a LOG file
              write-host "Error Deleting user from site collection: $($site.rootweb.url)`n" $_.Exception.Message
              $_.Exception.Message >> "d:\error.log" 
              $ErrorActionPreference = "Continue"

#Call the function
Delete-UserFromAllSites "" "global\davep" $true 

This will delete SharePoint user from all site collections in the given web application.

Delete Multiple Users from SharePoint site using PowerShell:
To delete multiple users from all site collections, just store user accounts in an array and call the PowerShell function. E.g.

#Array to store user accounts
$userNames= ("domain\user1", "global\salaudeen", "domain\user3")
#Iterate through the array
foreach($user in $userNames)
   #Call the function
   Delete-UserFromAllSites "" $user $true 

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

4 thoughts on “Delete Users from SharePoint Site Collection using PowerShell

  • Can you share a script by which I can delete all the users from site collections under a web application who have full control access?

  • I think you might need to clean up your SiteUrl examples…there’s lots of HTML showing up in them.

  • thanks for sharing. great info.


Leave a Reply

Your email address will not be published. Required fields are marked *