0

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!

Zirono
  • 89
  • 2
  • 2
  • 9
  • 1
    Have you considered using jobs? That could allow you to "multi-thread", as long as you aren't rate-limited by the server, it should work. – Jacob Colvin Jul 05 '17 at 15:17
  • Is your math is off? 10 users per second, you can remove 864,000 users in a day. 60 x 60 x 24 x 10. So you should be complete with the 50k in about 2 hours? Not bad - stick with your solution, mate. – Taterhead Jul 06 '17 at 06:17
  • @Taterhead You are right in your calculations, but I have to remove the 50,000 users from a total of 10,000 site collections. so the 864,000 users in a day divided by 50,000 users per site is about 17.3 sites per day. so we take the total number of sites (10,000) and divide by 17.3 which totals to about 578 days to remove all users from all sites :( – Zirono Jul 12 '17 at 16:32
  • Thanks @JacobColvin I am trying to figure out how to use Jobs to solve my problem. It is unfortunately getting more and more complicated as I go – Zirono Jul 12 '17 at 16:34
  • 1
    @Zirono Maybe also look at this, just saw it today :) https://github.com/nightroman/SplitPipeline – Jacob Colvin Jul 12 '17 at 16:35
  • @JacobColvin this looks really interesting, thanks! – Zirono Jul 12 '17 at 16:53

0 Answers0