So let’s say you have an environment with a bajillion Site Collections and some new hire comes along and wants to be a Site Collection Administrator to every Site Collection in your farm. And because your boss said so. What would you do?

A. Tell him no way jose!

B. Add him to each Site Collection as a Site Collection Administrator manually a bajillion times. And then file for worker’s comp.

C. Tell him to manually request access to each SharePoint site as he goes along.

D. It’s PowerShell time!

If you picked D, I applaud you. Now here’s a PowerShell script that’ll carry this out for you:

# This script will add or remove a named Site Collection Administrator
# to all Site Collections within a Web Application.
#
# Author: Henry Ong
######################## Start Variables ########################
$newSiteCollectionAdminLoginName = "domain\login"
$newSiteCollectionAdminEmail = "your.email@address.com"
$newSiteCollectionAdminName = "Your Display Name"
$newSiteCollectionAdminNotes = ""
$siteURL = "http://SharePointSiteURL" #URL to any site in the web application.
$add = 1 # 1 for adding this user, 0 to remove this user
######################## End Variables ########################
Clear-Host
$siteCount = 0
[system.reflection.assembly]::loadwithpartialname("Microsoft.SharePoint")
$site = new-object microsoft.sharepoint.spsite($siteURL)
$webApp = $site.webapplication
$allSites = $webApp.sites
foreach ($site in $allSites)
{
    
    $web = $site.openweb()
    $web.allusers.add($newSiteCollectionAdminLoginName, $newSiteCollectionAdminEmail, $newSiteCollectionAdminName, $newSiteCollectionAdminNotes)
    
    $user = $web.allUsers[$newSiteCollectionAdminLoginName]
    $user.IsSiteAdmin = $add
    $user.Update()
    $web.Dispose()
    $siteCount++
}
$site.dispose()
write-host "Updated" $siteCount "Site Collections."

Notes: Thanks to Google and Keith Richie for the assist.

Advertisements