Powershell Tips Series : How to add a user as a site collection admin on each and every My Site in SharePoint

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

SharePoint Office 365 Tool
Simple & Powerful Tool for Migration, Security & Reporting. Free Trial

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 name-my.sharepoint.com"
 # 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: "https://contoso-my.sharepoint.com"
         # ...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." 
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.

Protect Your SharePoint