How to add links to left navigation or quick launch using PowerShell to all SharePoint sites and subsites

Let us explore how to add left navigation or quick launch links using PowerShell to all SharePoint sites and subsites.

Add left navigation or quick launch links using PowerShell to all SharePoint sites

In SharePoint Online, we can easily add a link in the left navigation using PowerShell. It will add in all sites and subsites under a particular SharePoint site collection.

Here we will use the CSOM PowerShell, using Windows PowerShell ISE.

add links to left navigation using powershell
add links to left navigation using powershell

Below is the PowerShell script which will add links to left navigation into the SharePoint Online sites and subsites.


#Load SharePoint CSOM Assemblies
Add-Type -Path "D:\Microsoft.SharePoint.Client.dll"
Add-Type -Path "D:\Microsoft.SharePoint.Client.Runtime.dll"

$username = "user email id"
$password = "password"
$SiteCollectionUrl="http://siteurl"

function GetClientContext($SiteCollectionUrl, $username, $password) {
     $securePassword = ConvertTo-SecureString $password -AsPlainText -Force
     $context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteCollectionUrl) 
     $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword) 
     $context.Credentials = $credentials
     return $context
}




function catchsubsites ($subsiteurl){
    $clientContext = GetClientContext $subsiteurl $username $password
    $rootWeb = $clientContext.Web
    $childWebs = $rootWeb.Webs
    $clientContext.Load($rootWeb)
    $clientContext.Load($childWebs)
    $clientContext.ExecuteQuery()
    #do something on top level site
    #write-host $rootWeb.url -ForegroundColor Yellow

	
    foreach ($childWeb in $childWebs)
    {
	

        #do something for each subsite
        write-host $childWeb.url -ForegroundColor Yellow
	#Get the Quick launch Navigation of the web
$QuickLaunch = $childWeb.Navigation.QuickLaunch
$clientContext.load($QuickLaunch)
$clientContext.ExecuteQuery()
 
#Add link to Quick Launch Navigation
$NavigationNode = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation
$NavigationNode.Title = "New link"
$NavigationNode.Url = "https://LINK"
#$NavigationNode.AsLastNode = $False

$clientContext.Load($QuickLaunch.Add($NavigationNode))
$clientContext.ExecuteQuery()
		
        #see if there are any subsites beneath this and loop all of them too
        catchsubsites $childWeb.url
	
    }
}

catchsubsites $SiteCollectionUrl

Once you run the above script, the link will be added to the quick launch in all the SharePoint sites and subsites under the SharePoint site collection.

You may like the following SharePoint tutorials:

>