Using PowerShell to unify your SharePoint hub site search experience

When registering SharePoint Online sites with a hub site, the central search bar will remain scoped to each individual site rather than the hub by default. This is fine, but may lead to a slightly disjointed search experience in, for example, a company intranet where users might expect the search bar to behave the same across the entire intranet hub, and always bring back results from across the hub no matter what site they’re currently visiting.

Fortunately, it’s very easy to remedy this using a few short lines of PowerShell.

Full Script

$tenantName = "yourtenant"
$hubSiteName = "yourhubsitename"
Connect-SPOService -Url "https://$"$hubSite = Get-SPOHubSite | ? { $_.Title -eq $hubSiteName }$sites = Get-SPOSite -Limit All$registeredSites = $sites | ? { (Get-SPOSite $_.Url).HubSiteID.GUID -eq $hubSite.ID.GUID }$registeredSites | % { Connect-PnPOnline -Url $_.Url -UseWebLogin; $web = Get-PnPWeb -Includes SearchScope; if ($web.SearchScope -eq "Hub") { Write-Host "Web $($web.Url) already has hub site search scope"; return; } Write-Host "Setting hub site search scope for web $($web.Url)..."; $web.SearchScope = 2; $web.Update(); Invoke-PnPQuery; }

The final line of the script loops through each site registered to the hub and sets the SearchScope property on each one to 2 . This is what ensures the hub search scope at each site.

The SearchScope property can hold the following values:

# 1 for Tenant, 2 for Hub, 3 for Site, 0 for default behavior



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store