SharePoint Online: Add Top Navigation Link using PowerShell

Requirement: Add a link to the top navigation in SharePoint Online using PowerShell.

When creating or customizing a SharePoint Online site, you may need to add a link to the top navigation. This blog post will show you how to add a link in the top navigation bar in SharePoint Online. We will also show you how to use PowerShell to add a link in the top navigation bar of your SharePoint site.

On modern team and communication sites, It gives an easy interface to add links to the top menu. To add a new link to the top navigation in SharePoint Online, follow the below steps:

  1. Navigate to your SharePoint Online modern site and click on the “Edit” link at the end of the menu.
    add a link to top navigation menu in sharepoint online
  2. Hover over the Edit navigation pane, until you see a + sign appear.
  3. Click on the + sign to add a new link.
  4. Enter the URL and display the name for the new link in the appropriate fields.
    add a link in top navigation menu sharepoint online
  5. Click “Save” to add the new link to the top navigation bar.

That’s it! The new link should now appear in the top navigation bar on your SharePoint Online modern site.

Add a Link to Top Navigation in Classic sites

  1. Login to your SharePoint Online site as an Administrator or a user with Full Control.
  2. Click on Settings gear >> Site Settings
  3. Click on the “Top link bar” link under the “Look and Feel” group
  4. Now you can add a new link to the top navigation bar by entering the Name of the new link, and the URL.
    how to add a link to top navigation bar in sharepoint online

If your site template is a publishing site (or a site with a publishing feature enabled!), you’ll get a “Navigation” link under site settings. You can add links under “Global Navigation”.

How to add top navigation bar in SharePoint Online?
In modern Team sites, You can move the left navigation menu to the top navigation! Switch the Quick Launch to Top Navigation in SharePoint Online

Here is the PowerShell to add a link to the global navigation of the SharePoint Online site.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Add new Link in Top Navigation
Function Add-SPOTopNavigationLink()
{
    Param(
        [String]$SiteURL,
        [parameter(Mandatory=$false)][String]$ParentNodeTitle,
        [String]$Title,
        [String]$URL
    )

    #Setup Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get the Top Navigation of the web
    $TopNavigationBar = $Ctx.Web.Navigation.TopNavigationBar
    $Ctx.load($TopNavigationBar)
    $Ctx.ExecuteQuery()

    #Populate New node data
    $NavigationNode = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation 
    $NavigationNode.Title = $Title
    $NavigationNode.Url = $URL
    $NavigationNode.AsLastNode = $true

    #Get the Parent Node
    $ParentNode = $TopNavigationBar | Where-Object {$_.Title -eq $ParentNodeTitle}
    
    #Add New node to the navigation
    If($ParentNode -eq $null)
    {
        #Check if the Link with Title exists already
        $Node = $TopNavigationBar | Where-Object {$_.Title -eq $Title}
        If($Node -eq $Null)
        { 
            #Add Link to Root node of the Navigation
            $Ctx.Load($TopNavigationBar.Add($NavigationNode))
            $Ctx.ExecuteQuery()
            Write-Host -f Green "New Link '$Title' Added to the Navigation Root!"
        }
        Else
        {
            Write-Host -f Yellow "Navigation Link '$Title' Already Exists in Root!"
        }
    }
    else
    {
        #Get the Parent Node
        $Ctx.Load($ParentNode)
        $Ctx.Load($ParentNode.Children)
        $Ctx.ExecuteQuery()
 
        #Check if the Link with given title exists
        $Node = $ParentNode.Children | Where-Object {$_.Title -eq $Title}
        If($Node -eq $Null)
        { 
            #Add Link to Parent Node
            $Ctx.Load($ParentNode.Children.Add($NavigationNode))
            $Ctx.ExecuteQuery()
            Write-Host -f Green "New Navigation Link '$Title' Added to the Parent '$ParentNodeTitle'!"
        }
        Else
        {
            Write-Host -f Yellow "Navigation Link '$Title' Already Exists in Parnet Node '$ParentNodeTitle'!"
        }
    }
}
 
#Config Parameters
$SiteURL="https://Crescent.sharepoint.com/unitedstates"

#Call the function to Add a New Link in Root of the navigation
Add-SPOTopNavigationLink -SiteURL $SiteURL -Title "Support Center" -URL "https://support.crescent.com"

#Call the function to Add a link in "Support Center" node of the navigation
Add-SPOTopNavigationLink -SiteURL $SiteURL -ParentNodeTitle "Support Center" -Title "Application Support" -URL "https://suppport.crescent.com/apps"

Result of the script:

sharepoint online add link to top navigation using powershell

Use the Add-PnPNavigationNode cmdlet from PnP PowerShell to add a link to the top navigation or quick launch in SharePoint Online:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #Add a Link to Top Navigation Bar
    Add-PnPNavigationNode -Title "Support Center" -Url "https://support.crescent.com" -Location TopNavigationBar

    #Get the Navigation node "Support Center"
    $ParentID = Get-PnPNavigationNode -Location TopNavigationBar | Where {$_.Title -eq "Support Center"}  | Select -ExpandProperty ID
    #Add a link under "Support Center
    Add-PnPNavigationNode -Title "Application Support" -Url "https://support.crescent.com/apps" -Location TopNavigationBar -Parent $ParentID
 
    Write-host "Quick Launch Links Added Successfully!" -f Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

2 thoughts on “SharePoint Online: Add Top Navigation Link using PowerShell

  • how can I activate the option open in new tab?

    Reply
  • How to translate node’s Title with PNP ?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *