Clear the Quick Launch Programmatically Using PowerShell

by James Callaghan, Consultant@ClearPeople

21/03/2012

Today I had a requirement to remove all the headings and links from the quick launch of SharePoint sites (webs) as they were being created by a PowerShell deployment script.

SharePoint sites

I knew it was possible as I did something similar on a project with SharePoint 2007. So you would have thought that it would have been a relatively simple task, however I didn’t have access to this previous script and couldn’t quite remember how I achieved it.

In the end I did a little research and found some example PowerShell scripts which were about working with the quick launch. One post from Get-SPScripts did what I was after but was part of a much larger script. In the end this is what I ended up with:

function Clear-QuickLaunch($url){

$SPWeb = Get-SPWeb $url

$QuickLaunch = $SPWeb.Navigation.QuickLaunch

$Nodes = @()

$QuickLaunch | ForEach-Object {

$Nodes = $Nodes + $_.Id

}

$Nodes | ForEach-Object {

$Node = $SPWeb.Navigation.GetNodeById($_)

$Node.Delete()

}

}

Clear-QuickLaunch “http://sharepoint/”

SharePoint Management Shell

I got off to a rather slow start as I had forgot to set the $Nodes variable to an empty array and PowerShell kept throwing this error.

This was because $Nodes = $Nodes + $_.Id was performing a sum calculation rather than building up an array of all the objects. All was resolved once I remembered to include $Nodes = @() before using the $Nodes variable as an array.

SharePoint sites