SharePoint Pals
 | Sign In
Powershell Tips Series : How to add a user as a Site Collection Administrator in SharePoint Online
by Sriram Varadarajan 19 Dec 2015
Author
Enterprise Architect
Visits   
Today  :   9     Total  :    4623

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 
 
blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter November 21, 21:08
Implementing Multi-Threading with MaxDegreeOfParallelism in SharePoint using CSOM – An Insight - https://t.co/RNM7eVuyaa

Twitter November 21, 12:48
Configure Windows Firewall for SQL Server Analysis Service (SSAS) - https://t.co/re2H0cnGwn

Twitter November 21, 12:46
Implementing Multi-Threading in SharePoint To Upload Files using CSOM C# – An Insight - https://t.co/18QwUQG2eR

Twitter November 20, 21:48
How to Get the Client Context Using App Access token, by passing Client ID using CSOM in SharePoint Office 365 - https://t.co/wpmlJfTeWx

Twitter November 18, 21:30
How to Enable Auto Approval For Permission Requests in Community Site Template – Office 2013 using Client Side Object Model (CSOM) C#

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation