GitHub Icon Image
GitHub

Get All hub site and its main sites navigation nodes and update the navigation nodes if needed

Summary

This script shows how to get all hub site and its main sites navigation nodes and update the navigation nodes if needed.

Implementation

  • Open Windows PowerShell ISE
  • Create a new file
  • Write a script as below,
  • You need admin access to run this script
  • PnP PowerShell

# Connect to the SharePoint Admin site
$adminUrl = "https://[tenant]-admin.sharepoint.com"

Connect-PnPOnline -Url $adminUrl -UseWebLogin

# Get all main sites in the hub
$hubSiteUrl = "https://[tenant].sharepoint.com/sites/[hubsite]"
$mainSites = Get-PnPHubSiteChild -Identity $hubSiteUrl

# Initialize an array to hold the results
$myResults = @()

foreach ($site in $mainSites) {
    # Switch context to the main site
    Connect-PnPOnline -Url $site -UseWebLogin

    # Get top-level navigation nodes
    $navNodes = Get-PnPNavigationNode # -Location TopNavigationBar

    foreach ($node in $navNodes) {
        # Add parent node to results
        $myResults += [PSCustomObject]@{
            NodeCode = $node.Id
            NodeTitle = $node.Title
            NodeType = "Parent"
            URL      = $node.Url
        }

        # Get child nodes
        $childNodes = Get-PnPNavigationNode -Id $node.Id

        foreach ($childNode in $childNodes.Children) {

            # Check if the child node title is "Confluence" and change it to "Confluence2"
            if ($childNode.Title -eq "Confluence") {
                # Rename the child node to "Confluence2"
                  $childNode.Title = "Confluence2"
                  $childNode.Url = "/Lists/TestLlist"
                  $childNode.Update()
                  $childNode.Context.ExecuteQuery()
               
                $childNode.Title = "Confluence2" # Update local object for display
            }

            # Add child nodes to results
            $myResults += [PSCustomObject]@{
                NodeCode = $childNode.Id
                NodeTitle = $childNode.Title
                NodeType = "Child"
                URL      = $childNode.Url
            }
        }
    }

    Disconnect-PnPOnline
}

# Display the results in a formatted table
$myResults | Format-Table -AutoSize

Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell

The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?

Contributors

Author(s)
Valeras Narbutas

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Back to top Script Samples
Generated by DocFX with Material UI