SharePoint

PowerShell – Add or remove a site collection administrator from all sites

This is an issue I recently faced.  I needed to add myself as a site collection administrator to 100+ site collections.  I did not want to do this manually because it would have taken too much time.  So what did I do, I put it off until I got fed up with having to add myself manually each time I needed access.  I started searching for posts and found and excellent article on how to achieve this via PowerShell.

Here is the link to the original post: http://blog.henryong.com/2011/04/08/adding-or-removing-a-site-collection-administrator-from-all-site-collections/
This worked perfectly except for one minor detail….no progress indicator.  This is usually not that big of a deal since PowerShell takes care of business so quickly, but this time, I wanted to know where I was during each step of the process.  To do this, I added a loop counter to display the current percentage of operation.  Once the script has run, it will write back to your screen the exact number of site collections modified.

Thanks to Henry Ong over at The SharePoint Swiss Army Knife for the main script.

# This script will add a named Site Collection Administrator
# to all Site Collections within a Web Application.
#
######################## Start Variables ########################
$newSiteCollectionAdminLoginName = "domain\user"
$newSiteCollectionAdminEmail = "email@saltypc.com"
$newSiteCollectionAdminName = "Eric Kirkpatrick"
$newSiteCollectionAdminNotes = ""
$siteURL = "https://yourwebapplication" #Web Application URL
$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
######################## Write Progress Declaration ######################## 
$i = 0
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++

######################## Update Counter and Write Progress ########################
   $i++
   Write-Progress -Activity "Adding $newSiteCollectionAdminName to all site collections within $siteURL.  Please wait..." -status "Added: $i of $($allSites.Count)" -percentComplete (($i / $allSites.Count)  * 100)
}
$site.dispose()
write-host "Updated" $siteCount "Site Collections."

To remove someone from the site collection admins, simply change the variable $add = 1 to $add = 0.  What I did was create a separate script for removing and updated the verbage in the write progress to indicate that I was removing a user.

Stay Salty!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.