Copy Top Navigation between Hub Sites in SharePoint Online through PowerShell

 Office 365, SharePoint Online, Perumal
Aug 13 2018

In this blog, we shall check how to copy Top Navigation between Hub sites in SharePoint Online. Install SharePoint PnP Online PowerShell before running this script.

This PowerShell script uses SharePoint Online PnP cmdlet Get-PnPNavigationNode to get the top navigations and Add-PnPNavigationNode to add the top navigation to the given hub site.

Param(
[Parameter(ParameterSetName = "Inputparameter",Position=1,Mandatory=$True)]
[String]$SourceHubsiteUrl,
[Parameter(ParameterSetName = "Inputparameter",Position=2,Mandatory=$True)]
[String]$DestinationHubsiteUrl
)

Add-Type -AssemblyName PresentationFramework
Function Copy-NavigationToplink ($SourceHubsiteUrl,$DestinationHubsiteUrl)
{
    $GetTopNavs= Get-NavigationToplink -Hubsite $SourceHubsiteUrl 
    Add-NavigationToplink -Topnavs $GetTopNavs -Hubsite $DestinationHubsiteUrl
}

#Get Top Navigation from the given Hub Site
Function Get-NavigationToplink($Hubsite,$Credentials)
{
    [System.Windows.MessageBox]::Show("Provide the login credentials for ($Hubsite) Site")
    Connect-PnPOnline -Url $Hubsite -UseWebLogin
    $TopNavs=Get-PnPNavigationNode -Location TopNavigationBar |Select Title,Url 
    Disconnect-PnPOnline
    return $TopNavs
}

#Add the Top Navigation to the given Hub Site
Function Add-NavigationToplink($TopNavs,$Hubsite)
{
    [System.Windows.MessageBox]::Show("Provide the login credentials for ($Hubsite) Site")
    Connect-PnPOnline -Url $Hubsite -UseWebLogin
    foreach($TopNav in $TopNavs)
    {
        if($TopNav.Url)
        {
          Add-PnPNavigationNode -Location TopNavigationBar -Title $TopNav.Title -Url $TopNav.Url
        }
    }
}

Copy-NavigationToplink -SourceHubsiteUrl $SourceHubsiteUrl -DestinationHubsiteUrl $DestinationHubsiteUrl         
        
You can download the script directly from the PowerShell Gallery too. https://www.powershellgallery.com/packages/Copy-SPOHubSiteNavigation