In this article, we will see how to bulk update secondary Admin for My Sites or OneDrive for Business (ODFB) using PowerShell.
In some situations, you might need to add secondary site admin to gain access to a "OneDrive for Business" environment of a user as part of the governance. By default, each user is added as primary & secondary site collection administrators to their personal site or ODFB site collection.

Follow the below steps to see site collections administrators for a MySite.

SP Admin Center - User Profiles - Manage User Profile - Search for a user.

When a user is marked for deletion and if Access delegation is enabled in My Site settings of the SP Admin Center, the default action is to transfer the ownership to the Manager or Secondary Owner (in the absence of the Manager) to take control of the files in the absence of the user.

In some cases, no manager is assigned as well as no Secondary Admin. In such case, thesesites or ODFB will become orphans and get deleted after 30 days (default retention period).

However, as shown in the above screen, there is an option to enable My Site Secondary Admin but this only works for the new My Sites. For previously created My sites, it should be added individually which is fine for one or a few but hectic to add for all.
Use the below script to bulk update the secondary admin for all My Sites.
At a high level, below are the steps performed in the script.
  • Declare variable (configure variable according to your tenant)
  • Connect to SharePoint Online & Context

    • Create People Manager object to retrieve profile data

  • Connect to Azure Active Directory

    • Get User profiles. In the code I have provided two commands -- one to retrieve all licensed users and other to fetch a single user. You should comment the code based on your need.

  • Load user profile using profile manager and retrieve PersonalSpace URL (My site URL)
  • Set secondary admin
  • Export to CSV file.
  1. #Pre-Requisites, Install below modules
  2. #Sharepoint online Management Shell : https://www.microsoft.com/en-us/download/details.aspx?id=35588
  3. #Azure Active Directory http://connect.microsoft.com/site1164/Downloads/DownloadDetails.aspx?DownloadID=59185
  4. #SharePoint Online Client Components SDK https://www.microsoft.com/en-us/download/details.aspx?id=42038
  5. Clear-Host
  6. #Specify tenant admin and URL
  7. $AdminAccount = '[email protected]'
  8. $TenantURL = 'https://company-admin.sharepoint.com'
  9. #Specify the secondary admin account and the url for the onedrive site
  10. $Secondaryadmin = '[email protected]'
  11. $MySiteURL = 'https://company-my.sharepoint.com'
  12. #Use this varable to apply seconday site collection for a specific user
  13. $User = '[email protected]'
  14. #Location to save the report
  15. $UserProfileOutPut = 'D:\MyWokingFolder\Report\AllProfiles.csv'
  16. #Attention: sometimes folder path may be 15 or 16. Browse the folder and verify the availability of the dlls
  17. #Add references to SharePoint online client component assemblies
  18. Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
  19. Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
  20. Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll'
  21. Write-Host "Loading SharePoint Assemblies..." -ForegroundColor Yellow
  22. Write-Host "Connecting to SharePoint Online Service and Context..." -ForegroundColor Yellow
  23. $Password = Read-Host -Prompt 'Please enter your password' -AsSecureString
  24. $Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $AdminAccount, $Password
  25. $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminAccount,$Password)
  26. #Bind to Site Collection
  27. $Context = New-Object Microsoft.SharePoint.Client.ClientContext($TenantURL)
  28. $Context.Credentials = $Creds
  29. Write-Host "Connected to SharePoint Online Context..." -ForegroundColor Yellow
  30. #Create People Manager object to retrieve profile data
  31. $PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Context) -ErrorAction Inquire
  32. Write-Host "Loading People Manager..." -ForegroundColor Yellow
  33. #Connect to Office 365 tenant
  34. try
  35. {
  36. Connect-MsolService -Credential $Credentials -ErrorAction Inquire
  37. Write-Host "Connected to SharePoint Online Service..." -ForegroundColor Yellow
  38. }
  39. catch
  40. {
  41. Write-Host "Unable to Connect to SharePoint Online...Existing the Script."
  42. return
  43. }
  44. #Use below code to apply seconday site collection for all licensed users
  45. $Users = Get-MsolUser -All | where {$_.isLicensed -eq $true}
  46. #Use below code to apply seconday site collection for a specific user
  47. #$Users = Get-MsolUser -All | where {$_.UserPrincipalName -eq $User}
  48. Write-Host "Collecting Users Information from SharePoint Online..." -ForegroundColor Yellow
  49. $Headings = ""
  50. $boolCreateHeadings = $true
  51. Connect-SPOService -Url $TenantURL -Credential $Credentials
  52. Foreach ($User in $Users)
  53. {
  54. $ClaimsUserFormat = 'i:0#.f|membership|'+ $User.UserPrincipalName
  55. $UserProfile = $PeopleManager.GetPropertiesFor($ClaimsUserFormat)
  56. $Context.Load($UserProfile)
  57. $Context.ExecuteQuery()
  58. #Allow profiles only with PersonalSpace URL
  59. If ($UserProfile.UserProfileProperties['PersonalSpace'] -ne $null)
  60. {
  61. $PersonalSpace = $UserProfile.UserProfileProperties['PersonalSpace'];
  62. $PersonalSpace = $MySiteURL + $PersonalSpace
  63. $temp = Set-SPOUser -Site $PersonalSpace -LoginName $secondaryadmin -IsSiteCollectionAdmin $true
  64. Write-Host "Added secondary admin to the site ($PersonalSpace)"
  65. if($boolCreateHeadings)
  66. {
  67. Write-Host "Loading CSV Headings..." -ForegroundColor Green
  68. $Headings = '" FirstName "," LastName "," UserName "," PersonalSpace "'
  69. $Headings -join "," | Out-File -Encoding default -FilePath $UserProfileOutPut
  70. $boolCreateHeadings = $false
  71. }
  72. $Properties = '"' + $UserProfile.UserProfileProperties["FirstName"] + '",' + '"' + $UserProfile.UserProfileProperties["LastName"] + '",' + '"' + $UserProfile.UserProfileProperties["UserName"] + '",' + '"' + $UserProfile.UserProfileProperties["FirstName"] + '",' + '"' + $UserProfile.UserProfileProperties["PersonalSpace"] + '"';
  73. #Export to CSV.
  74. $Properties -join "," | Out-File -Encoding default -Append -FilePath $UserProfileOutPut
  75. Write-Host "User Profile Written to CSV $UserProfileOutPut" -ForegroundColor Yellow
  76. }
  77. }
  78. Write-Host "Successfully assigned seconday site collection admin.All profiles have been Written to $UserProfileOutPut" -ForegroundColor Green
To run the script without errors, the below prerequisites must be met.
Output Screen when it is run to update secondary admin for a specific user -

References to some issues.
  • Execution of scripts is disabled on this system

    • Set the execution policy to remote signed using below command
Set-ExecutionPolicy RemoteSigned
I hope you find this informative.