Clear the Quick Launch programmatically using PowerShell
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.
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){
$QuickLaunch = $SPWeb.Navigation.QuickLaunch
$QuickLaunch | ForEach-Object {
$Nodes | ForEach-Object {
$Node = $SPWeb.Navigation.GetNodeById($_)
Clear-QuickLaunch "http://sharepoint/"
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.
Contributed by: James Callaghan