PowerShell – Navigation and multiple site collections in SharePoint 2010

Running a large SharePoint farm? Multiple site collections? Need to create your global navigation to span across all site collections?

With PowerShell you can script the creation of your navigation to add / modify / and delete nodes.  If you’re running multiple site collections you can loop through each site collection within a web application to update the navigation.  In the example below we’ll create 1 standard top navigation node, and another node with 1 dropdown element.
 
//Name of the web application
$webapp = Get-SPWebApplication http://intranet
//Loop through each site collection within the web application
foreach ($SPSite in $webapp.sites)
    {
        //Open the web
        $OpenWeb = $SpSite.OpenWeb()
        //Select the top navigation bar
        $TP = $OpenWeb.Navigation.TopNavigationBar
        //Create a new object, this will be your navigation element
        $Node = New-ObjectMicrosoft.SharePoint.Navigation.SPNavigationNode “Link 1”,http://link1,1
        //Add the navigation element to the top navigation bar, this will be added as the last item
        $TP.AddAsLast($node)
        //Create a new navigation element with 1 dropdown node
        $Node = New-ObjectMicrosoft.SharePoint.Navigation.SPNavigationNode
Resourcehttp://resource,1
        $Node1 = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode“Dropdown”, “http://Dropdown“,1
        //Add the ‘Resources’ node to the top navigation
        $TP.AddAsLast($Node)
        //Add the ‘Dropdown’ node to the Resources tab
        $Node.Children.AddAsLast($Node1)
        //Cleanup
        $SPSite.Dispose()
        $OpenWeb.Dispose()
      }