Thursday, July 4, 2013

PowerShell code to delete bulk users from SharePoint site

here is a simple code sample of PowerShell script to delete users from SharePoint site.

Function DeletePermissions($web)
{
         $usersToDelete = @("Domain\UserName")
         # 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)
            {
                $UserName = $user.Name
                if($arrUsers -notcontains $UserName.ToLower() -and $UserName -notlike "*svc_*")
                {
                    Echo $user.Name
                    $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();
}


1 comment:

  1. Thanks for the Code. There are some more possible 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#.

    Found these methods at SharePointDiary.com: Delete Users from SharePoint Site Collection .

    ReplyDelete