Remove SharePoint 2007 Top Navigation using PowerShell.

Following on from Steve's great post yesterday about creating site Navigation from CSV it got me thinking, what If I want to use this to create the Global Navigation on a farm that already has a hotch-potch TopNav structure?

If this is the situation you are in then the following PowerShell is your friend.. Essentially it runs through all the site collections in your farm and removes all of the TopNav apart from the initial 'Home' Tab.

You could also use this PS to clean up if you have used Steve's Script in a test environment and you are trying to figure out which Nav structure suits you best..... or if you got the .csv input wrong :)

Thanks goes to Steve for all his help :)  

WARNING - THIS WILL DELETE ALL OF THE TOP NAVIGATION BAR FOR ALL OF YOUR SITE COLLECTIONS IN ALL OF YOUR WEB APPLICATIONS WITH A SINGLE CLICK.

Enjoy..

Andy

 

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

    $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
    $webServices = $farm.Services;
   
    foreach ($webService in $farm.Services)
    {
        if (!($webService -is [Microsoft.SharePoint.Administration.SPWebService]))
        {
            continue;
        }

 foreach ($webApp in $webService.WebApplications | Where-Object {($_.DefaultServerComment -ne 'SharePoint Central Administration v3')})
        {
            if ($webApp -is [Microsoft.SharePoint.Administration.SPAdministrationWebApplication])
            {
                continue;
            }

$siteCollection = $webApp.Sites     
           
foreach ($site in $siteCollection)
{
 
$web=$site.OpenWeb()
$tn = $web.Navigation.TopNavigationBar
[int]$count=$tn.Count                   
while($count -ne 0)
{
  if ($tn[$count-1].Title -ne "Home")
  {
    $tn[$count-1].Delete()
  }
  $count--
}
}
}
}