SharePoint Pals
 | Sign In
Powershell Tips Series : How to add a user as a site collection admin on each and every My Site in SharePoint
by Sriram Varadarajan 19 Dec 2015
Enterprise Architect
Today  :   13     Total  :    5126

This code snippet would be helpfull when you try to add a user as site collection administrator to multiple sites. The list of sites that needs to be processed can be passed via text file as input.

Code Snippet

 # URL for your organization's SPO admin service
 # User account for an Office 365 global admin in your organization
 $AdminAccount = "TENANT USER ID"
 # Compliance manager to be made site collection admin on each MySite
 $eDiscoveryUser = "USER NAME"
 # URL for your tenant's MySite domain
 $MySitePrefix = "https://your organization"
 # Where should we read the list of MySites?
 # This file should contain partial MySite paths formatted as follows, one per line; for example
 # /personal/junminh_contoso_onmicrosoft_com/
 $MySiteListFile = 'C:\Users\youralias\Desktop\ListOfMysites.txt'
 # Begin by connecting to the service
 Connect-SPOService -Url $AdminURI -Credential $AdminAccount
 # Make a reader for our list of MySites
 $reader = [System.IO.File]::OpenText($MySiteListFile)
 try {
     for(;;) {
         # Read a line
         $line = $reader.ReadLine()
         # Stop if it doesn't exist
         if ($line -eq $null) { break }
         # Turn the line into a complete SharePoint site path by merging $MySitePrefix
         # Formatted like this: ""
         # ...with each partial MySite path in the file, formatted like this:
         # "/personal/junminh_contoso_onmicrosoft_com/"
         $fullsitepath = "$MySitePrefix$line"
         Write-Host "Operating on $fullsitepath "
         # We need to remove the last "/" to work around an issue.
         # "/personal/junminh_contoso_onmicrosoft_com/"
         # becomes "/personal/junminh_contoso_onmicrosoft_com"
         $fullsitepath = $fullsitepath.trimend("/")
         # Make the specified eDiscovery user a site collection admin on the OneDrive for Business site
         Write-Host "Making $eDiscoveryUser a Site Collection Admin"
         Set-SPOUser -Site $fullsitepath -LoginName $eDiscoveryUser -IsSiteCollectionAdmin $true
 finally {
 Write-Host "Done!"
 Write-Host "Log written." 
blog comments powered by Disqus

SharePoint 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


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 in a word document.

Related Resources

Recent Tweets

Twitter October 23, 22:21
How to Add/Remove User Custom Actions (in Site Actions Menu) Programmatically using CSOM PNP in SharePoint -

Twitter October 21, 21:34
How to Add a JS Link Reference to the Display Form or Any other ASPX Programmatically using CSOM PNP in SharePoint -

Twitter October 20, 13:01
How to Add a JS Link Reference to the NewForm and EditForm Programmatically using CSOM PNP in SharePoint Office 365-

Twitter October 12, 12:15
How to Deploy Provider Hosted Apps (Add-Ins) by App Stapling in SharePoint Office 365 -

Twitter October 11, 13:39
How to Deploy Provider HostedApp programmatically using CSOM in SharePoint Office 365 Activating Developer Feature -

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