When we are starting or doing development on SharePoint online most of the time, and since the introduction of the Hub Site (hubify), projects are done with this new structure. Adding a new user as admin on each site of the Hub will be very time consuming. With the PowerShell Script shared here, this task can be done in a few seconds.
In order to run this script successfully, you require Global SharePoint administrator rights. Furthermore, a site belonging to the Hub needs to be added, as well as the email of the person to be added as an admin on your Hub.
Make sure that you also have all the SharePoint Online Management Shell installed on your desktop. You can open your PowerShell as an Administrator and paste the next line to install all the required cmdlets.
1 Install-Module -Name Microsoft.Online.SharePoint.PowerShell2
The following Power Shell script will make a user an admin of your Hub (replace the required values).
1$adminCenter = "https://contoso-admin.sharepoint.com" #SharePoint admin center2$hubSite = "https://contoso.sharepoint.com/sites/hubcontonso" #Hub Site root3$username = "contoso@contoso.com" #Your email4$password = "password" #Your password5$adminEmail = "contosoAdmin@contoso.com" #New Admin6$encpassword = convertto-securestring -String $password -AsPlainText -Force7$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $encpassword8Connect-SPOService -Url $adminCenter -Credential $cred9$hub = Get-SPOHubSite $hubSite10$sites = Get-SPOSite -Limit All11$sitesFromHub = New-Object System.Collections.ArrayList12Write-Host ("Searching {0} sites" -f $hub.Title) -BackgroundColor Gray -ForegroundColor Black13foreach ($site in $sites){14 try{15 $hubSiteID = Get-SPOHubSite $site.Url16 if($hubSiteID.ID -eq $hub.ID){17 Write-Host ("* {0} - {1} ... " -f $site.Title, $site.Url) -NoNewline18 $sitesFromHub.add($site) | Out-Null19 Write-Host "Done" -BackgroundColor Green -ForegroundColor Black20 }21 }22 catch{23 continue24 }25}26Write-Host Write-Host ("Adding {0} as Admin:" -f $adminEmail) -BackgroundColor Gray -ForegroundColor Black27foreach ($hubSiteAssociated in $sitesFromHub){28Write-Host ("* {0} - {1} ... " -f $hubSiteAssociated.Title, $hubSiteAssociated.Url) -NoNewline29try{30 Set-SPOUser -Site $hubSiteAssociated -LoginName $adminEmail -IsSiteCollectionAdmin $true | Out-Null31 Write-Host "Done" -BackgroundColor Green -ForegroundColor Black32}33catch{34 Write-Host "No Permissions" -ForegroundColor Black -BackgroundColor Red35 }36}37Write-Host "All Done"38Write-Host "Press any key to Close..."39$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")40
Conclusion
This script will add the user as an admin on the SharePoint sites that are associated with the Hub. This script can be useful when you're developing the Hub sites and keeping the content updated on SharePoint sites.
David Ramalho
SharePoint Developer at BindTuning