{"id":86,"date":"2011-11-16T09:00:20","date_gmt":"2011-11-16T14:00:20","guid":{"rendered":"http:\/\/blogs.visigo.com\/chriscoulson\/?p=86"},"modified":"2011-11-16T09:00:20","modified_gmt":"2011-11-16T14:00:20","slug":"bulk-delete-sharepoint-site-users-with-powershell","status":"publish","type":"post","link":"https:\/\/blogs.visigo.com\/chriscoulson\/bulk-delete-sharepoint-site-users-with-powershell\/","title":{"rendered":"Bulk Delete SharePoint Site Users with PowerShell"},"content":{"rendered":"<p>Below is a PowerShell script for deleting a filtered list of users from a SharePoint site. \u00a0Simply 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.<\/p>\n<p>The script is based on a combination of the scripts from:<\/p>\n<p><a href=\"http:\/\/blogs.msdn.com\/b\/vijgang\/archive\/2009\/04\/26\/powershell-script-to-remove-all-users-from-a-sharepoint-group.aspx\">http:\/\/blogs.msdn.com\/b\/vijgang\/archive\/2009\/04\/26\/powershell-script-to-remove-all-users-from-a-sharepoint-group.aspx<\/a><\/p>\n<p>and<\/p>\n<p><a href=\"http:\/\/nikspatel.wordpress.com\/2010\/08\/10\/delete-orphaned-ad-users-from-the-site-collection\/\">http:\/\/nikspatel.wordpress.com\/2010\/08\/10\/delete-orphaned-ad-users-from-the-site-collection\/<\/a><\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n################################################################################################################\r\n\r\n&#x5B;System.Reflection.Assembly]::Load(&quot;Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot;)\r\n&#x5B;System.Reflection.Assembly]::Load(&quot;Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot;)\r\n&#x5B;System.Reflection.Assembly]::Load(&quot;Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot;)\r\n&#x5B;System.Reflection.Assembly]::Load(&quot;System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a&quot;)\r\n\r\n###########################\r\n# &quot;Enter the site URL here&quot;\r\n$SITEURL = &quot;http:\/\/demo2010a:2114&quot;\r\n\r\n# &quot;Enter the username filter (lowercase) here&quot;\r\n$USERNAMEFILTER = &quot;member&quot;\r\n\r\n###########################\r\n\r\n$site = new-object Microsoft.SharePoint.SPSite ( $SITEURL )\r\n$web = $site.OpenWeb()\r\n&quot;Web is : &quot; + $web.Title\r\n\r\n$usersToDelete = @()\r\n\r\n# Iterate through the site's users and add usernames to\r\n# an array of users to delete if the name contains\r\n# contains the username filter.\r\nforeach ($user in $web.SiteUsers)\r\n{\r\n\tif ($user.LoginName.ToLower().Contains( $USERNAMEFILTER ))\r\n\t{\r\n\t\t$usersToDelete += $user.LoginName\r\n\t}\r\n}\r\n\r\n# Delete each user selected from the SiteUsers array.\r\n# The SiteUsers array can't be iterated through directly\r\n# as it gets changed as users are removed from it.\r\nforeach ($user in $usersToDelete)\r\n{\r\n\t&quot;Removing user : &quot; + $user\r\n\t$web.SiteUsers.Remove($user);\r\n}\r\n\r\n$web.Update();\r\n\r\n################################################################################################################\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Below is a PowerShell script for deleting a filtered list of users from a SharePoint site. \u00a0Simply 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182,5],"tags":[188,187,185,179,135,186,22,24,189,183,184,190],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-administration","category-sharepoint","tag-bulk-delete","tag-bulk-remove","tag-delete","tag-delete-users","tag-powershell","tag-remove","tag-sharepoint-2","tag-sharepoint-2010","tag-site-users","tag-siteurl","tag-siteusers","tag-user-list"],"_links":{"self":[{"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":0,"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}