Introduction

In this blog, I have discussed how to add, retrieve, and delete the SharePoint navigation menu using PowerShell. Follow the steps to get the result.

Steps

  1. Get the Navigation Node
  2. Add the Navigation
  3. Remove/Delete the Navigation
In this blog, I added the coding of HubNavigation Menu but you can also Add, Retrieve and Delete the footer, Top and Hub Navigation menu from your site by running the script.

Get Top Navigation Node

Follow the below code to get the Hub Navigation menu.
  1. $siteURL = Read - Host "Enter site url"
  2. try {
  3. Connect - PnPOnline - Url $siteURL - Credentials(Get - Credential)
  4. #Get Hub Navigation and Its Sub Menu
  5. $topNavsMenu = Get - PnPNavigationNode - Location TopNavigationBar | Select Title, Url, Id
  6. foreach($topNav in $topNavsMenu) {
  7. Write - Host "Top Navigaion : "
  8. $topNav.Title
  9. $node = Get - PnPNavigationNode - Id $topNav.Id
  10. $childMenus = $node.Children | Select Title, Url, Id
  11. if ($childMenus - ne $null) {
  12. foreach($childMenus in $node.Children) {
  13. Write - Host "SubNavigation : "
  14. $childMenus.Title
  15. $subNodeMenus = Get - PnPNavigationNode - Id $childMenus.Id
  16. $subchilds = $subNodeMenus.Children | Select Title, Url, Id
  17. if ($subchilds - ne $null) {
  18. foreach($childMenus in $subchilds) {
  19. Write - Host $childMenus.Title
  20. }
  21. }
  22. }
  23. }
  24. }
  25. } catch {}
It is retrieving Hub navigation top, sub & child menu.
Normal 0 false false false EN-IN X-NONE X-NONE
Get,Add & Remove Navigation Using PnP-Powershell
Get,Add & Remove Navigation Using PnP-Powershell
For Quick Launch Menu,
  1. $quicklunch = Get-PnPNavigationNode -Location QuickLaunch
  2. Write-Host $quicklunch.Title
For Footer Navigation,
  1. $footerNav = Get-PnPNavigationNode -Location Footer
  2. Write-Host $footerNav.Title
For Search Navigation,
  1. $searchNav = Get-PnPNavigationNode -Location SearchNav
  2. Write-Host $searchNav.Title

Add Navigation Node

Follow the code mentioned below to add the navigation node
  1. $siteUrl = Read - Host "Enter site url"
  2. try {
  3. Connect - PnPOnline - Url $siteUrl - Credentials(Get - Credential)
  4. Add - PnPNavigationNode - Location TopNavigationBar - Title "HubNavigation" - Url "https://www.google.com"
  5. } catch {}
Get,Add & Remove Navigation Using PnP-Powershell

Remove Navigation Node

Follow the code mentioned below to remove navigation node.
  1. $siteUrl = Read - Host "Enter site url"
  2. try {
  3. Connect - PnPOnline - Url $siteUrl - Credentials(Get - Credential)
  4. $navMenus = Get - PnPNavigationNode - Location TopNavigationBar | Select Title, Url, Id
  5. $deleteNavNode = $navMenus | Where - Object {
  6. $_.Title - eq "HubNavigation"
  7. }
  8. Remove - PnPNavigationNode - Identity $deleteNavNode.Id - Force
  9. } catch {}
Get,Add & Remove Navigation Using PnP-Powershell

Conclusion

Here we have given the dynamic way to add, retrieve, and delete the SharePoint online navigation menu. As we are using the PowerShell PnP this it's easier to perform this as well as more time effective.