Friday, August 8, 2014

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."

 

I hope the above information will help you to resolve the issue, in case of any queries/questions regarding the above mentioned information then please let me know. I would be more than happy to help you as well as resolves your issues, Thank you.

Related Posts Plugin for WordPress, Blogger...

ShareThis

X