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.
- Copy the script in notepad and save as .PS1
- Do modifications as said below
- Open power shell as Admin,Ensure your in the same location where Script is copied.
- Run ./Script.ps1
- You see a below screen once its run successfully
Modifications: You need to do below modifications according to your requirements.
- $newSiteCollectionAdminLoginName = "Name of user"
- $newSiteCollectionAdminEmail = " user mail ID"
- $newSiteCollectionAdminName = " Name of user "
- $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.