Remove All Users from SharePoint Site or List with PowerShell

Requirement: There is a SharePoint 2013 list with a lot of user permissions, and I wanted to remove all user permissions from that SharePoint list. So tried removing all users, but SharePoint gave “You have chosen to delete too many groups at once. Select fewer groups and try again.” when choosing the “All users” check box and remove them from the SharePoint site or group.

You have chosen to delete too many groups at once. Select fewer groups and try again.

So, the problem here is: We can’t delete users in bulk when it exceeds the maximum count by SharePoint. Here are my nifty PowerShell scripts to remove all users from the SharePoint site or List.

PowerShell script to remove all user permission from SharePoint list:

Let me show you how to use PowerShell to delete all users from a SharePoint list.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$SiteUrl="https://your-sharepoint-site.com" 
$ListName="Your-List-Name" 

#get the Web
$web = Get-SPWeb $SiteUrl
#Get the List
$list = $web.Lists[$ListName]

#If the List is not using Unique permissions, Break inheritance
if(!$list.HasUniqueRoleAssignments)
{
    #Break list inheritance - without copying users
    $list.BreakRoleInheritance($false)
}

#Remove all users from the list
 for ($i = $list.RoleAssignments.Count - 1; $i -ge 0; $i--)
    {
      $RoleAssignment = $list.RoleAssignments[$i]
      $list.RoleAssignments.RemoveByID($RoleAssignment.Member.ID)
      write-host "Removed User/Group :" $RoleAssignment.Member.name
    } 

Remove all users from the SharePoint site

Similarly, to delete all users from a SharePoint site, use this PowerShell:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Site collection URL 
$SiteUrl="https://demo.sp.cloudservicesme.com" 

#get the Root Web
$web = Get-SPWeb $SiteUrl

#Get All Users of the site collection
$UserAccounts = @()
foreach ($user in $web.SiteUsers)
{
   $UserAccounts = $UserAccounts + $user.loginname
}

#Remove all users one by one.
foreach ($user in $UserAccounts)
{
    try
    {
        #Set the Error Action
        $ErrorActionPreference = "Stop"

        #Remove User if not site admin
        if(!$web.SiteUsers[$User].isSiteAdmin)
        {
            $web.SiteUsers.Remove($user)
            Write-host "User Removed :" $user -ForegroundColor Green
        }
    }
    catch    
    {
        Write-host "Failed to remove the user:" + $user -ForegroundColor Red   
    }
    Finally
    {
        #Reset the Error Action to Default
        $ErrorActionPreference = "Continue"
    } 
}

This can be useful if you need to clean up your SharePoint list or site by removing everyone’s direct permissions to it.

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!

One thought on “Remove All Users from SharePoint Site or List with PowerShell

  • Can I get a script from which I can remove access of over 3000 users from a web application??

    Reply

Leave a Reply

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