Bulk Delete SharePoint Site Users with PowerShell

Below is a PowerShell script for deleting a filtered list of users from a SharePoint site.  Simply copy the script to a .ps1 file, adjust the $SITEURL to the url of the site and adjust the $USERNAMEFILTER to a lowercase string that is contained in all of the usernames you would like to delete.

The script is based on a combination of the scripts from:

http://blogs.msdn.com/b/vijgang/archive/2009/04/26/powershell-script-to-remove-all-users-from-a-sharepoint-group.aspx

and

http://nikspatel.wordpress.com/2010/08/10/delete-orphaned-ad-users-from-the-site-collection/

################################################################################################################

[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

###########################
# "Enter the site URL here"
$SITEURL = "http://demo2010a:2114"

# "Enter the username filter (lowercase) here"
$USERNAMEFILTER = "member"

###########################

$site = new-object Microsoft.SharePoint.SPSite ( $SITEURL )
$web = $site.OpenWeb()
"Web is : " + $web.Title

$usersToDelete = @()

# Iterate through the site's users and add usernames to
# an array of users to delete if the name contains
# contains the username filter.
foreach ($user in $web.SiteUsers)
{
	if ($user.LoginName.ToLower().Contains( $USERNAMEFILTER ))
	{
		$usersToDelete += $user.LoginName
	}
}

# Delete each user selected from the SiteUsers array.
# The SiteUsers array can't be iterated through directly
# as it gets changed as users are removed from it.
foreach ($user in $usersToDelete)
{
	"Removing user : " + $user
	$web.SiteUsers.Remove($user);
}

$web.Update();

################################################################################################################

Comments

4 responses to “Bulk Delete SharePoint Site Users with PowerShell”

  1. Tim N Avatar

    How about:

    $web.SiteUsers.RemoveCollection($web.SiteUsers)

    I recently used this to clear up all of my (unnecessary) site collections ACL’s which was wreaking havoc on the crawler… worked like a charm =)

  2. SPAdam Avatar
    SPAdam

    Thanks! But there are multiple ways to Delete Users from SharePoint Site Collection:

    1. You can delete users using SharePoint Web Interface
    2. Delete users from SharePoint site using PowerShell (Bulk Delete also possible)
    3. Remove users from SharePoint programmatically using C#

    Find all possible ways at: http://www.sharepointdiary.com/2013/07/delete-users-from-site-collection-using-powershell.html#ixzz2aXghqIQb

  3. C Avatar
    C

    hi, I’m having trouble modifying this for multiple users across multiple site collections.

    1. Chris Coulson Avatar

      You’ll have to loop through every site collection in the web application and run this logic. If you have multiple web applications you’ll have to loop through them too. This link shows looping through all web applications and site collections:

      http://kancharla-sharepoint.blogspot.ca/2013/08/powershell-script-to-get-all-site.html

Leave a Reply to C Cancel reply

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