Add Site Collection Administrator in Sharepoint Online using Powershell

By default, the user who is owner of the site collection is the only person who has site collection admin rights on it. This can be a problem for compliance and e-discovery reasons. You might be in a situation where you need to give site collection admin rights to a compliance manager or a global administrator. In this article, I am going to write PowerShell script to set a Site Collection administrator in Sharepoint Online/Office 365 using CSOM library.

Before proceed, you need to download the SharePoint Server 2013 Client Components SDK. Adjust the path of reference Assembly as necessary in below script.

Replace the following variables with your own values:

  • $siteUrl
  • $siteAdmin
  • $siteAdminPwd
  • $newAdmin
[Reflection.Assembly]::LoadFrom("C:\SharepointCSOMMicrosoft.SharePoint.Client.dll")
[Reflection.Assembly]::LoadFrom("C:\SharepointCSOMMicrosoft.SharePoint.Client.Runtime.dll")
 
$siteUrl = “https://mts.sharepoint.com/sites/MySiteCollection” 
$siteAdmin = "[email protected]" 
$siteAdminPwd ='MyPa@@word';
$password = New-Object System.Security.SecureString;
$siteAdminPwd.ToCharArray() | ForEach-Object {$password.AppendChar($_)};
  
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)  
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($siteAdmin, $password)  
$ctx.Credentials = $credentials 

$newAdmin = '[email protected]'
$spUser = $ctx.Web.EnsureUser($newAdmin); 
$spUser.IsSiteAdmin = $true; 
$spUser.Update();
 
$ctx.Load($spUser) 
$ctx.ExecuteQuery()

Note: An existing site collection administrator can alone add another administrator.


Advertisement

Leave a Comment