Share via


Script to add or remove a user as site collection administrator in many site collections

Script to add or remove a user as site collection administrator in many site collections

Description: many times as a SharePoint administrator we required to perform a task that to add a user as administrator in many site collections. This is very difficult if has 100’s of site collections. In this article I am providing the script how to achieve this with one click.

Instructions: Follow the Instructions below to run successfully.

  1. Copy the script in notepad and save as .PS1
  2. Do modifications as said below
  3. Open power shell as Admin,Ensure your in the same location where Script is copied.
  4. Run ./Script.ps1
  5. You see a below screen once its run successfully

Modifications: You need to do below modifications according to your requirements.

  1. $newSiteCollectionAdminLoginName = "Name of user"
  2. $newSiteCollectionAdminEmail = " user mail ID"
  3. $newSiteCollectionAdminName = " Name of user "
  4. $siteURL = " siteURL " #URL to any site in the web application.

Script:

# This script will add named Site Collection Administrator
# to all Site Collections within a Web Application.
#
# Author: Anil Avula
######################## Start Variables ########################
$newSiteCollectionAdminLoginName = ""Name of user "
$newSiteCollectionAdminEmail = " user mail ID "
$newSiteCollectionAdminName = " Name of user "
$newSiteCollectionAdminNotes = ""
$siteURL = " siteURL " #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."

Note: I resolved the issue by above Method. If you find a different solution, please report it as a comment to this post. This will be useful to SharePoint Community. Be sure to double-verify it: undo your solution and verify that the problem comes back, then redo it and verify that the problem goes away.

See Also

  1. SharePoint Resources on the TechNet Wiki
  2. SharePoint 2013 Portal
  3. SharePoint 2013 - Service Applications
  4. SharePoint 2013 - Resources for Developers
  5. SharePoint 2013 - Resources for IT Pros