removing a list of users from all site collections in a web application

579 Views Asked by At

Here is the basis of my question, I am trying to remove a very large list of users from all site collections in multiple web applications. My overall goal is to remove roughly 50,000 users from almost 10,000 site collections (all site collections in about 15 web applications).

I currently have a way of doing this by having a list of users and a list of sites, and simply looping through the sites and removing each user from each site.

I'm wondering if there is a more efficient way of doing this, and or if there is a way to remove a user from a web application and have that cascade down through each of the site collections.

Below is what I currently have:

$users = get-content $userList
$sites = get-content $siteList
Write-Host "Getting all listed sites"
Write-Host "Getting all listed users"
$siteArray = @()
for ($i=0; $i -lt $sites.length; $i++)
{
    try
    {
        $rmUserCount = 0
        $userNotExist = 0
        $failedRm = 0
        Write-Host "Working on" $sites[$i]"..."
        for ($c=0; $c -lt $users.length; $c++)
        {
            Write-Host "Attempting to remove user" $users[$c] -ForegroundColor Yellow
            try
            {
                write-host $isuser
                $isUser = Get-SPUser -Identity $users[$c] -web $sites[$i] -ErrorAction SilentlyContinue                                       
            }
            catch
            {

            }

            if($isUser)
            {
                try
                {
                    Remove-SPUser -Identity $users[$c] -Web $sites[$i] -Confirm:$false
                    Write-Host "The user" $users[$c] "was removed from the site" $sites[$i] -ForegroundColor Green
                    $rmUserCount++
                }
                catch
                {
                    Write-Host "The user still exists and was not removed" -ForegroundColor Red
                    $failedRm++
                }
            }
            else
            {
                $userNotExist++
                Write-Host "user" $users[$c] "does not exist on" $sites[$i]"... moving on"
            }
            $isuser=$null
        }
        $addInArrayOut = $rmUserCount.ToString() + " users removed from " + $sites[$i].ToString() + ", " + $userNotExist.ToString() + " users did not exist on the site, " + $failedRm.ToString() + " users failed to be removed"
        $siteArray += $addInArrayOut

    }
    catch
    {
         Write-Host $_ $Error[0] -ForegroundColor Red
    }
}

From my calculations using measure command, I can remove roughly 10 users per second, but this still means that it would take hundreds of days to complete this task, leaving me thinking that there must be a better way of doing this.

I tried adding the web application url as one of the sites in the array and it didn't work like I thought it would, as it didn't remove the user from each of the site collections within it.

If anyone has any idea of how to do this better, or a way of drastically speeding up what I have I would really appreciate it. Thanks!

0

There are 0 best solutions below