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?
- To delete a user in SharePoint 2010, Navigate to the site collection as a Site Collection Administrator
- Click on Site actions >> Site permissions
- Click on any SharePoint group. Now the URL will look something like this:
https://your-site-collection-url/_layouts/people.aspx?MembershipGroupID=11. - 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. - Select the user you want to delete, and click on Actions >> “Delete user from Site collection”
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 "https://sharepoint.crescent.com/sites/operations"
#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 = "https://sharepoint.crescent.com/sites/operations";
string userAccount = @"Global\MarkM";
using (SPSite site = new SPSite(siteURL))
{
using (SPWeb web = site.OpenWeb())
{
//Get the User
SPUser user=null;
try
{
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
web.SiteUsers.Remove(user.LoginName);
Console.WriteLine("User removed from:"+site.RootWeb.Url);
}
}
}
}
//Pause
Console.ReadLine();
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)
{
<#
.SYNOPSIS
Function to Search and Delete given user from all site collections of the web application
.DESCRIPTION
This PowerShell function Scans each site collection under all site collections of the given web application
and deletes the user from site collections.
.EXAMPLE
DeleteUserFromAllSites "https://sharepoint.crescent.com" "global\davep" $true
This example removes the user account "global\davep" from the web application "https://sharepoint.crescent.com"
.INPUTS
$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
.OUTPUTS
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)
{
try
{
$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)"
}
else
{
#Remove the User from site collection
$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)`n" $_.Exception.Message
$_.Exception.Message >> "d:\error.log"
}
finally
{
$ErrorActionPreference = "Continue"
$site.Dispose()
}
}
}
#Call the function
Delete-UserFromAllSites "https://sharepoint.crescent.com" "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 "https://sharepoint.crescent.com" $user $true
}
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.
Done, Thanks!
thanks for sharing. great info.