I was recently asked to write a PowerShell script to remove all users from SharePoint 2007. In SharePoint 2007, you cannot remove a user from a collection that you are currently iterating. To get around this, we create an array to store the user names in and then iterate the array and remove the users. Not all of the users can be removed. If a user is the owner of the site, it will generate an exception. The code below lets you know the status of each user as it attempts the removal.

# Variables to modify per environment 

$siteURL = “http://lab2007.ps4sp.com

#[void][system.reflection.assembly]::load(“Microsoft.sharepoint, version=, culture=neutral, publickeytoken=71e9bce111e9429c”)

$site = new-object Microsoft.SharePoint.SPSite($siteURL)

$web = $site.rootweb

$list = @()

foreach ($user in $web.SiteUsers)


   $list = $list + $user.loginname


foreach ($user in $list)

   if ($user -ne “”)



         “Successfully removed ” + $user



      “Failed to remove ” + $user + ” may be an administrtor or is the owner of a Web Site Collection.”






Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s