Associate Multiple Site Collections to Hub Site
Summary
This PowerShell script can be used to associate mutilple site collections to Hub site. You can provide list of site collection URLs in an array.
Implementation
- Open Windows PowerShell ISE
- Create a new file
- Update the parameters with site collection URLs and hub site URL
- Save the file and run it
# Parameters
# Provide SharePoint online Hub site URL
$HubSiteURL = "https://******.sharepoint.com/sites/**********"
# Array of site collections to associate with hub site
$arrSCs = @("https://******.sharepoint.com/sites/**********", "https://******.sharepoint.com/sites/**********", "https://******.sharepoint.com/sites/**********")
# Get admin user credentials
$creds = (Get-Credential)
function AssociateHubSite {
try {
foreach ($SC in $arrSCs) {
Write-Host "Connecting site collection: " $SC
Connect-PnPOnline -Url $SC -Credentials $creds
Add-PnPHubSiteAssociation -Site $SC -HubSite $HubSiteURL -ErrorAction Stop
Write-Host "Hub site associated with site collection: " $SC -ForegroundColor Green
}
}
catch {
Write-Host "Error in associating hub site $($SC): " $_.Exception.Message -ForegroundColor Red
}
# Disconnect SharePoint online connection
Disconnect-PnPOnline
}
AssociateHubSite
Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell
The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?
Contributors
Author(s) |
---|
Siddharth Vaghasia |
Ganesh Sanap |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.