kwizcom banner advertisement

SharePoint Online: Add Site Collection Administrator using PowerShell

SharePoint site collection administrators have Full access rights to manage all sites under a site collection. When creating a site collection, Global Administrator or SharePoint Online Administrator will automatically become the primary site collection admin. A SharePoint online site collection can have several administrators, but only one primary administrator, unlike SharePoint on-premises.

Its possible to add multiple site collection admins for any SharePoint site collection. To add additional site collection administrator in SharePoint online, you must be either Global Administrator or SharePoint online Site Collection Administrator of the particular site collection.

SharePoint online add site collection administrator
  • Navigate to your SharePoint Online administration center (E.g. https://crescent-admin.sharepoint.com/_layouts/15/online/SiteCollections.aspx )
  • Select the site collection, Click on "Owners" button from the ribbon and then click on "Manage Administrators" menu item.
    sharepoint online powershell add site collection administrator
  • This brings "Manage Administrators" page where you can add multiple site collection administrators. Also provides the option to change primary site collection administrator.
    sharepoint online powershell set site collection administrator
  • Now you can add new admins into the Site Collection Administrators box.
  • Once you have added the new user press OK to save the changes and any new Site Collection Administrator will have full rights to every site and sub-site within that Site Collection
Alternatively, Once the site collection is created, You can also navigate to Site Settings >> Site collection administrators link to add additional site collection administrators.
sharepoint online add site collection administrator powershell

Quite easy, isn't it? Well, Its easy for one single site collection. However there is a problem when you have large number of site collections. Say, you have 100's of SharePoint online site collections, You can't simply select all of your site collections and add a site collection administrator to all of them at one shot!

In SharePoint Online, site collection administrators to be added on a site collection by site collection basis, as there is no web application level users policies can be set from Central Administration as we do in SharePoint on-premises.

So, the solution is: Using PowerShell to add site collection administrator in SharePoint online!

PowerShell Script to Add Site Collection Administrator in SharePoint Online:
Here is the PowerShell for SharePoint online to add site collection administrator
#Variables for processing
$AdminURL = "https://crescent-admin.sharepoint.com/"
$AdminName = "salaudeen@crescent.onmicrosoft.com"
$SiteCollURL = "https://crescent.sharepoint.com/sites/Sales/"
$SiteCollectionAdmin = "mark@crescent.onmicrosoft.com"

#User Names Password to connect 
#$SecurePWD = read-host -assecurestring "Enter Password for $AdminName" 
$SecurePWD = ConvertTo-SecureString "Password1" –asplaintext –force  
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $SecurePWD
 
#Connect to SharePoint Online
Connect-SPOService -url $AdminURL -credential $Credential

#Add Site collection Admin
Set-SPOUser -site $SiteCollURL -LoginName $SiteCollectionAdmin -IsSiteCollectionAdmin $True

Add Site collection Admin to All SharePoint Online Sites using PowerShell:
SharePoint online PowerShell to add site collection administrator for all site collections.
#Variables for processing
$AdminURL = "https://Crescent-admin.sharepoint.com/"
$AdminName = "SPAdmin@Crescent.com"
 
#User Names Password to connect 
$Password = Read-host -assecurestring "Enter Password for $AdminName" 
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Password

#Connect to SharePoint Online
Connect-SPOService -url $AdminURL -credential $Credential

$Sites = Get-SPOSite -Limit ALL

Foreach ($Site in $Sites)
{
    Write-host "Adding Site Collection Admin for:"$Site.URL
    Set-SPOUser -site $Site -LoginName $AdminName -IsSiteCollectionAdmin $True
}
To Remove a Site Collection Admin, use Set-SPOUser cmdlet with -IsSiteCollectionAdmin $false parameter! Here is how: Remove Site Collection Administrator in SharePoint Online with PowerShell

Change Primary Site Collection Administrator using PowerShell:
SharePoint Online PowerShell to set site collection administrator
#Variables for processing
$AdminURL = "https://crescent-admin.sharepoint.com/"
$AdminName = "salaudeen@crescent.onmicrosoft.com"
$SiteCollURL = "https://crescent.sharepoint.com/sites/Sales"
$NewSiteAdmin = "mark@crescent.onmicrosoft.com"

#User Names Password to connect 
$SecurePWD = ConvertTo-SecureString "Password1" –asplaintext –force  
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $SecurePWD
 
#Connect to SharePoint Online
Connect-SPOService -url $AdminURL -credential $Credential

#Change Site Collection Primary Admin
Set-SPOSite -Identity $SiteCollURL -Owner $NewSiteAdmin -NoWait

Update: Add Site collection Administrator to Group/Modern Sites
SharePoint modern sites are not listed under site collections (or even with SharePoint PowerShell module!). Here is the script to use in "SharePoint Online Management Shell" to add site collection admins to all sites including Modern Team sites.
#Variables for processing
$AdminURL = "https://crescent-admin.sharepoint.com/" 
$AdminName="SPAdmin@crescent.com"

#Connect to SharePoint Online
Connect-SPOService -url $AdminURL -credential (Get-Credential)

#Get All Site Collections
$Sites = Get-SPOSite -Limit ALL

#Loop through each site and add site collection admin
Foreach ($Site in $Sites)
{
    Write-host "Adding Site Collection Admin for:"$Site.URL
    Set-SPOUser -site $Site.Url -LoginName $AdminName -IsSiteCollectionAdmin $True
}

Add Site Collection Administrator using CSOM-PowerShell:
Other than SharePoint Online Management shell, we can also use PowerShell CSOM method to add a user to site collection administrator group. Here is how:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Variables for Processing
$SiteURL = "https://crescent.sharepoint.com/Sites/marketing"
$UserAccount="i:0#.f|membership|Salaudeen@crescent.com"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred

$User = $Ctx.Web.EnsureUser($UserAccount)
$User.IsSiteAdmin = $True
$User.Update()
$Ctx.ExecuteQuery()

Related Articles:
SharePoint Online: Add Site Collection Administrator using PowerShell SharePoint Online: Add Site Collection Administrator using PowerShell Reviewed by Salaudeen Rajack on August 01, 2015 Rating: 5

5 comments:

  1. Note: Get-SPOSite does not return sites created by O365 Groups (& Planner) or Teams, unless you specifically specify the URL of the site... so no enumeration of those sites :(

    ReplyDelete
    Replies
    1. Not true. Get-SPOSite does get me SP Sites that belong to an Office Group

      Delete
  2. How to remove site collection administrators in sharepoint online using powershell.

    ReplyDelete
  3. Thank you!!
    Very happy!!! :D

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.