SharePoint Online: Update Quick Launch Links using PowerShell

Requirement: Update Quick Launch in SharePoint Online using PowerShell.

The Quick Launch has been a popular way to organize and quickly access site content in all versions of SharePoint. But what if you want to update a link to a particular page or file on your site with a new one? Well, This can be accomplished using PowerShell and is relatively easy to do. This blog post will show you how to update the links in the Quick Launch toolbar using PowerShell.

To update links in quick launch, follow these steps:

  1. Go to Site Settings >> Click on “Quick launch” from Look and feel.
  2. Click on the “Edit” icon to edit and update the quick launch link.
powershell to update quick launch links in sharepoint online

If the publishing feature is enabled, You’ll get the “Navigation” link in site settings under “Look and Feel”

sharepoint online powershell update quick launch

SharePoint Online PowerShell to Update Quick Launch:

If you need to update the links in the Quick Launch for multiple SharePoint Online sites, it can be a time-consuming and tedious task to do manually. Fortunately, PowerShell can be used to automate this process and save time. Let’s discuss updating Quick Launch links in SharePoint Online using PowerShell.

Let’s replace an old link in the left navigation with a new link:

#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"
   
#Config Parameters
$SiteURL="https://Crescent.sharepoint.com/sites/marketing"
$OldURL = "https://support.crescent.com/app"
$NewURL = "https://support.crescent.com/applications"

#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 Quick launch Navigation of the web 
$QuickLaunch = $Ctx.Web.Navigation.QuickLaunch
$Ctx.load($QuickLaunch)
$Ctx.ExecuteQuery()

#Loop through Each link in Quick Launch Navigation
ForEach($QuickLaunchLink in $QuickLaunch)
{
    $Ctx.Load($QuickLaunchLink)
    $Ctx.Load($QuickLaunchLink.Children)
    $Ctx.ExecuteQuery()

    If($QuickLaunchLink.Url -eq $OldURL)
    {
        $QuickLaunchLink.Url = $QuickLaunchLink.Url.Replace($OldURL,$NewURL)
        $QuickLaunchLink.Update()
        $Ctx.ExecuteQuery()
        Write-Host -f Green "Quick Launch Link: '$($QuickLaunchLink.Title)' Updated!"
    }

    #Get Second level Links
    Foreach($Link in $QuickLaunchLink.Children)
    {
        $Ctx.Load($Link)
        $Ctx.ExecuteQuery()
          
        If($Link.Url -eq $OldURL)
        {
            $Link.Url = $Link.Url.Replace($OldURL,$NewURL)
            $Link.Update()
            $Ctx.ExecuteQuery()
            Write-Host  -f Green "Quick Launch Link '$($Link.Title)' Updated!"
        }
    }
} 

Let’s wrap the script into a function and search and replace quick launch links for all site collections in SharePoint Online tenant.

#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"

Function Update-SPOQuickLaunchLinks([Microsoft.SharePoint.Client.Web]$Web, $OldURL, $NewURL)
{   
    Try {
        Write-host -f Yellow "Searching Quick Launch Links in:" $($Web.url)
        $Ctx = $Web.Context
        #Get the Quick launch Navigation of the web 
        $QuickLaunch = $Web.Navigation.QuickLaunch
        $Ctx.load($QuickLaunch)
        $Ctx.ExecuteQuery()

        #Loop through Each link in Quick Launch Navigation
        ForEach($QuickLaunchLink in $QuickLaunch)
        {
            $Ctx.Load($QuickLaunchLink)
            $Ctx.Load($QuickLaunchLink.Children)
            $Ctx.ExecuteQuery()

            If($QuickLaunchLink.Url -eq $OldURL)
            {
                $QuickLaunchLink.Url = $QuickLaunchLink.Url.Replace($OldURL,$NewURL)
                $QuickLaunchLink.Update()
                $Ctx.ExecuteQuery()
                Write-Host -f Green "`tQuick Launch Link: '$($QuickLaunchLink.Title)' Updated!"
            }

            #Get Second level Links
            Foreach($Link in $QuickLaunchLink.Children)
            {
                $Ctx.Load($Link)
                $Ctx.ExecuteQuery()
          
                If($Link.Url -eq $OldURL)
                {
                    $Link.Url = $Link.Url.Replace($OldURL,$NewURL)
                    $Link.Update()
                    $Ctx.ExecuteQuery()
                    Write-Host  -f Green "`tQuick Launch Link '$($Link.Title)' Updated!"
                }
            }
        }
    }
    Catch {
        write-host -f Red "Error Updating Quick Launch Links!" $_.Exception.Message
    }
}

#Function to get each subsite in site collection
Function Get-SPOWebs($SiteURL)
{
    $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 web
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.Load($Web.Webs)
    $Ctx.ExecuteQuery()

    #Call the function to Search & Replace Quick launch links
    Update-SPOQuickLaunchLinks -Web $web -OldURL $OldURL -NewURL $NewURL

    #Loop through each each subsite in site
    Foreach($Web in $web.Webs)
    {
        Get-SPOWebs $Web.Url
    }
}

#Variables
$AdminCenterURL = "https://Crescent-admin.sharepoint.com"
$OldURL = "https://support.crescent.com/app"
$NewURL = "https://support.crescent.com/applications"

#Setup Credentials to connect
$Cred= Get-Credential
 
#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL -Credential ($Cred)

#Get all Site collections
$Sites = Get-SPOSite -Limit All

#Loop through site collections
ForEach($Site in $Sites)
{
    #Call the function to get all sites in the site collection
    Get-SPOWebs($Site.URL)
}

The same script can be used for updating Top navigation links as well. Just replace $Web.Navigation.QuickLaunch with “$Web.Navigation.TopNavigationBar”

Similarly, we can also update the quick launch link in SharePoint Online using PnP PowerShell.

#Config Variables
$SiteURL = "https://crescent.sharepoint.com"
$LinkTitle = "Support Center"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the Context
$Context =  Get-PnPContext

#Get the Quick Launch Navigation
$NavigationNodeCollection = Get-PnPNavigationNode -Location QuickLaunch

#Get the Link to Update
$NavigationNode = $NavigationNodeCollection | Where-Object { $_.Title -eq  $LinkTitle}
If($NavigationNode)
{
    #Update the Link URL
    $NavigationNode.Url = "https://support.crescent.com"
    $navigationNode.Update() 
    $Context.ExecuteQuery()
    Write-host "Navigation Link has been updated!"
}

By using the script given in this article, you can update Quick Launch links in your SharePoint Online sites quickly and efficiently!

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

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