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:
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(); ################################################################################################################
Leave a Reply