Add a user as an admin on all associated SharePoint Sites on a Hub

Escrito por  David Ramalho

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.PowerShell
2

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 center
2$hubSite     = "https://contoso.sharepoint.com/sites/hubcontonso" #Hub Site root
3$username    = "contoso@contoso.com" #Your email
4$password    = "password" #Your password
5$adminEmail  = "contosoAdmin@contoso.com" #New Admin
6$encpassword = convertto-securestring -String $password -AsPlainText -Force
7$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $encpassword
8Connect-SPOService -Url $adminCenter -Credential $cred
9$hub = Get-SPOHubSite $hubSite
10$sites = Get-SPOSite -Limit All
11$sitesFromHub = New-Object System.Collections.ArrayList
12​Write-Host ("Searching {0} sites" -f $hub.Title) -BackgroundColor Gray -ForegroundColor Black
13foreach ($site in $sites){
14       try{
15              $hubSiteID = Get-SPOHubSite $site.Url
16              if($hubSiteID.ID -eq $hub.ID){
17                    Write-Host ("* {0} - {1} ... " -f $site.Title, $site.Url) -NoNewline
18                    $sitesFromHub.add($site) | Out-Null
19                    Write-Host "Done" -BackgroundColor Green -ForegroundColor Black
20              }
21       }
22       catch{
23              continue
24       }
25}
26Write-Host Write-Host ("Adding {0} as Admin:" -f $adminEmail) -BackgroundColor Gray -ForegroundColor Black
27foreach ($hubSiteAssociated in $sitesFromHub){
28Write-Host ("* {0} - {1} ... " -f $hubSiteAssociated.Title, $hubSiteAssociated.Url) -NoNewline
29try{  
30       Set-SPOUser -Site $hubSiteAssociated -LoginName $adminEmail -IsSiteCollectionAdmin $true | Out-Null
31       Write-Host "Done" -BackgroundColor Green -ForegroundColor Black
32}
33catch{
34       Write-Host "No Permissions" -ForegroundColor Black -BackgroundColor Red
35 }
36}
37Write-Host "All Done"
38Write-Host "Press any key to Close..."
39$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
40

Image 1.- Output generated by the Script execution.

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