Powershell Tips Series : How to add a user as a Site Collection Administrator in SharePoint Online


Sriram Varadarajan
Enterprise Architect
Published On :   19 Dec 2015
Visit Count
Today :  6    Total :   5442
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


This powershell code snippet allows you to add a user as site collection administrator in SharePoint online

 # add a user or users to the site collection admin role on every site collection in Office 365 sites (SharePoint Online)
  
 #setup a log path
 $path = "$($(get-location).path)\LogFile.txt"
 
 start-transcript -path $Path
 write-host "This will connect to SharePoint Online"
  
 #Admin Variables:
 $Adminurl = "TENANT ADMIN URL"
 $username = "TENANT PASSWORD"
  
 #Tenant Variables:
 $TenantURL = "TENANT URL"
  
 $SiteCollectionAdmins = @("PROVIDED ALL THE USER IN COMMA SEPRATE ")
  
 #Connect to SPO
 $SecurePWD = read-host -assecurestring "Enter Password for $username"
 $credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $SecurePWD
  
 Connect-SPOService -url $Adminurl -credential $credential
 write-host "Connected" -foregroundcolor green
  
  
 $sites = get-sposite
 Foreach ($site in $sites)
 {
     Write-host "Adding users to $($site.URL)" -foregroundcolor yellow
 	#get the owner group name
 	$ownerGroup = get-spoSitegroup -site $site.url | where {$_.title -like "*Owners"}
 	$ownertitle = $ownerGroup.title
 	Write-host "Owner Group is named > $ownertitle > " -foregroundcolor cyan
 	
 	#add the Site Collection Admin to the site in the owners group
 	foreach ($user in $SiteCollectionAdmins)
 	{
 		Write-host "Adding $user to $($site.URL) as a user..."
 		add-SPOuser  -site $site.url -LoginName $user -group $ownerTitle
 		write-host "Done"
 		
 		#Set the site collection admin flag for the Site collection admin
 		write-host "Setting up $user as a site collection admin on $($site.url)..."
 		set-spouser -site $site.url -loginname $user -IsSiteCollectionAdmin $true
 		write-host "Done"	-foregroundcolor green
 	}
 }
 Write-host "Done with everything" -foregroundcolor green 
 stop-transcript 
 
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

Migratiin Tools for SharePoint