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

Sriram Varadarajan
 
Solution Architect
December 19, 2015
 
Rate this article
 
Views
9086

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

 Start-Transcript
 
 # URL for your organization's SPO admin service
 $AdminURI = "TENANT ADMIN URL"
 
 # 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:UsersyouraliasDesktopListOfMysites.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 {
     $reader.Close()
 }
 
 Write-Host "Done!"
 Stop-Transcript
 Write-Host "Log written." 
 

Author Info

Sriram Varadarajan
 
Solution Architect
 
Rate this article
 
Sriram is a Technology Evangelist with 15+ years experience in Microsoft Technologies. He is an enterprise architect working for large pharmaceutical organization which has presence globally with largest Microsoft implementation ...read more
 

Leave a comment