Add Site Collection Administrators Programmatically/PowerShell in SharePoint

SharePoint site collection administrators have Full rights to all sites within a site collection. Primary site collection administrator and Secondary site collection administrator specified in central administration will automatically become site collection admins. Its also possible to add multiple site collection admins for any SharePoint site collection.

How to add a site collection administrator in SharePoint?

To add a site collection administrator in SharePoint, you must be either a Farm Administrator or another Site Collection Administrator of the site collection. Navigate to Site Settings >> Click on the “Site collection administrators” link under “Users and Permissions”

add site collection administrator programmatically

which will lead you to a page where you can manage site collection administrators.

How to Add Site Collection Administrators Programmatically with C#?

In some scenarios, we may have to add site collection administrators programmatically. Say for e.g. There may be a requirement to add a particular user as site collection administrator to multiple site collections. Here is the code to add site collection administrator programmatically:

//Define the parameter values: Site collection URL and user account to remove
string siteURL = "https://sharepoint.crescent.com/sites/operations";
string userAccount = @"global\Salaudeen";

using(SPSite site=new SPSite(siteURL))
{
	using (SPWeb web = site.RootWeb)
	  {
		//Get the User 
		 SPUser user = web.EnsureUser(userAccount);

		//Make the user as Site collection Admin
		 user.IsSiteAdmin = true;
		 user.Update();

		//Print a message
		Console.WriteLine("User: "+userAccount +" has been added as site collection administrator!");
	   }
}

//Pause
Console.ReadLine();

Add site collection administrator SharePoint with PowerShell

The above code can be transformed to PowerShell to add site collection administrator SharePoint 2010 programmatically. Here is how to add site collection administrator in SharePoint 2010 using PowerShell:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Define the parameter values: Site collection URL and user account to remove
$siteURL = "https://sharepoint.crescent.com/sites/operations"
$userAccount = "Global\Salaudeen"

#Get the RootWeb
$web= Get-SPWeb $siteURL
#Get the user acount - If doesn't exists ADD
$user = $web.EnsureUser($userAccount)

#Make the user as Site collection Admin
$user.IsSiteAdmin = $true
$user.Update()

#Print a message
Write-host "User: $($userAccount) has been added as site collection administrator!"  

So with the Power of scripting, we can add site collection administrator to all sites.

Add site collection administrator using stsadm:
We can also add a site collection admin for MOSS 2007 using STSADM tool. Here is an example:
stsadm.exe -o adduser -url “https://sharepoint.crescent.com/sites/operations” -userlogin “Global\Salaudeen” -useremail “Salaudeen@crescent.com” -role “Full Control” -username “Salaudeen Rajack” -siteadmin

If you are looking for a way to set SharePoint site collection’s Primary administrator or secondary administrator, here is another post for you: How to Change Site Collection Primary, Secondary Administrators in SharePoint

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

One thought on “Add Site Collection Administrators Programmatically/PowerShell in SharePoint

  • Thanks!! This post really helped.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *