CSOMOffice 365PowerShellSharePoint Online

Delete Navigation (Top Navigation or Quick Launch Node) Using CSOM and PowerShell


A colleague asked me a script for deleting nodes from Global Navigation or from Quick Launch in a SharePoint Site. Before we get into the PowerShell script lets know about the Navigation (In General). The below image illustrates the navigation settings in a SharePoint Site


All the headings with folder signs are parent nodes and links are children i.e child nodes. The requirement for now is to remove the parent node which can be accomplished by enumerating the appropriate collections QuickLaunch and TopNavigationBar. Here is the full script

Import-Module C:\Scripts\CSOMOnlineAssemblies\Microsoft.SharePoint.Client.dll
Import-Module C:\Scripts\CSOMOnlineAssemblies\Microsoft.SharePoint.Client.Runtime.dll

function Remove-xSPONavigationNode 
   PowerShell script to delete the parent node from Quick Launch or from Global Navigation
   This PowerShell script is to delete the parent node including the child nodes from Quick Launch i.e (Current Navigation)
   or from Global Navigation (Top Level Navigation bar) 
   Remove-xSPONavigationNode -Url https://contoso.sharepoint.com -Title 'Tasks' -NavigationType QuickLaunch -Credential 'admin@contoso.onmicrosoft.com'
   Delete the node name Tasks and child nodes from the given site (Quick Launch)
   Remove-xSPONavigationNode -Url https://contoso.sharepoint.com -Title 'Subsite' -NavigationType GlobalNavigation -Credential 'admin@contoso.onmicrosoft.com'
   Delete the node name Subsite and child nodes from the given site (Global Navigation)
Delete Navigation (Top Navigation or Quick Launch Node) Using CSOM and PowerShell
.NOTE @ChendrayanV http://chen.about-powershell.com #> param ( [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)] $Url, [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)] $Title, [Parameter(Mandatory)] [ValidateSet('QuickLaunch' ,'GlobalNavigation')] $NavigationType, [Parameter(Mandatory)] [System.Management.Automation.CredentialAttribute()] [pscredential] $Credential ) process { try { $SPOClientContext = [Microsoft.SharePoint.Client.ClientContext]::new($Url) $SPOClientContext.Credentials = [Microsoft.SharePoint.Client.SharePointOnlineCredentials]::new($Credential.UserName,$Credential.Password) switch ($NavigationType) { "QuickLaunch" { $QuickLaunchNodes = $SPOClientContext.Web.Navigation.QuickLaunch $SPOClientContext.Load($QuickLaunchNodes) $SPOClientContext.ExecuteQuery() $SPOClientContext.Dispose() for($i=$QuickLaunchNodes.Count-1;$i -ge 0; $i--) { if($QuickLaunchNodes[$i].Title -eq $Title) { $QuickLaunchNodes[$i].DeleteObject() } } $SPOClientContext.ExecuteQuery() $SPOClientContext.Dispose() } "GlobalNavigation" { $GlobalNavigationNodes = $SPOClientContext.Web.Navigation.TopNavigationBar $SPOClientContext.Load($GlobalNavigationNodes) $SPOClientContext.ExecuteQuery() $SPOClientContext.Dispose() for($i=$GlobalNavigationNodes.Count-1;$i -ge 0; $i--) { if($GlobalNavigationNodes[$i].Title -eq $Title) { $GlobalNavigationNodes[$i].DeleteObject() } } $SPOClientContext.ExecuteQuery() $SPOClientContext.Dispose() } } } catch { $_.Exception.Message } } }

How to use this script? Copy paste the code in your script location for example C:\Scripts and follow the below steps. Warning: If you have deleted the node it can’t be restored.

PS C:\Scripts> . .\Remove-xSPONavigationNode.ps1

Remove-xSPONavigationNode -Url https://contoso.sharepoint.com -Title 'Subsite' -NavigationType GlobalNavigation -Credential $Credential

Remove-xSPONavigationNode -Url https://contoso.sharepoint.com -Title 'Tasks' -NavigationType QuickLaunch -Credential $Credential

'Subsite2' , 'Subsite3' | Remove-xSPONavigationNode -Url https://contoso.sharepoint.com -NavigationType GlobalNavigation -Credential $Credential

$Credential = Get-Credential -Credential ‘admin@contoso.onmicrosoft.com’ – By doing this we can pass multiple site and node title and remove the nodes from different sites

'Subsite2' , 'Subsite3' | Remove-xSPONavigationNode -Url https://contoso.sharepoint.com -NavigationType GlobalNavigation -Credential $Credential

I have removed few nodes from Top and Quick and the below image illustrates it. (Compare the preceding image)


In my next blog I will share few more scripts for SharePoint Online which may helps content managers to manage the site easily. Enjoy PowerShell!

Leave a Reply

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