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  :   2     Total  :    5768

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 January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services

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