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 :  1    Total :   6913

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