DEV Community

Tony Watters
Tony Watters

Posted on • Updated on

How to change menu items within SharePoint Online with PnP PowerShell

Introduction

Changing menu navigation items within SharePoint sites is a tedious manual step, it can take a long time to update the menu items throughout numerous sites. This is where PnP PowerShell comes in to make your work/life easy.

This tutorial will show you how to remove/add a new menu item to multiple SharePoint sites using the power of PnP PowerShell cmdlets.

PnP Powershell Overview

SharePoint Patterns and Practices (PnP) contains a library of PowerShell commands (PnP PowerShell) that allows you to perform complex provisioning and artifact management actions towards SharePoint. The commands use CSOM and can work against both SharePoint Online as SharePoint On-Premises.
https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/readme.md

The fun begins

First, we need to connect to the tenant of your current web

$tenantUrl = "https://xxx.sharepoint.com/"
Enter fullscreen mode Exit fullscreen mode

Next, we will get your sign in credentials with Get-Credential

$cred = Get-Credential
Enter fullscreen mode Exit fullscreen mode

In the next line of code, we will put our site page names with in an array called $sites

$sites = "volvo", "ford", "opel", "lada",
Enter fullscreen mode Exit fullscreen mode

Then we will create a for each loop that will create a variable called $name to loop through the array of $sites

foreach($name in $sites) {
Enter fullscreen mode Exit fullscreen mode

Here is where the nuts and bolts of the SharePoint PnP PowerShell come in.
We connect to the PnPOnline and add our -Url with our previously created variable $tenantUrl then concat the previously created $name variable to sites/$name then our Get-Credentials $Cred variable which logs us in to the SharePoint Online.

Connect-PnPOnline -Url $tenantUrl/sites/$name -Credentials $cred 
Enter fullscreen mode Exit fullscreen mode

We then loop through all the sites Navigation Nodes and find a “-Title” called "My old Cars" and remove it from the all the $sites Navigations entirely.

Remove-PnPNavigationNode -Location QuickLaunch -Title "My old Cars" -Force 
Enter fullscreen mode Exit fullscreen mode

Next, we can add our new Navigation Page link called “My New Cars” to our navigation menu. We find the location Quicklaunch then add a new -Title called "My New Cars" with the specific -Url.

Add-PnPNavigationNode -Location QuickLaunch -Title My New Cars -Url 'SitePages/MyNewCars.aspx'
}
Enter fullscreen mode Exit fullscreen mode

After running you will see that the old link has been removed and a new link node has been added to your navigations.

See full code below.

$tenantUrl = https://YOURSITE.sharepoint.com 
$cred = Get-Credential 
$sites = "volvo", "ford", "opel", "lada", 
foreach($name in $sites) {

    Connect-PnPOnline -Url $tenantUrl/sites/mycar-$name -Credentials $cred 
    Remove-PnPNavigationNode -Location QuickLaunch -Title "My old Cars" -Force 
    Add-PnPNavigationNode -Location QuickLaunch -Title My New Cars -Url 'SitePages/MyNewCars.aspx' 
}
Enter fullscreen mode Exit fullscreen mode

Hope this helps any aspiring PowerShell ninjas out there :)

Top comments (4)

Collapse
 
oscarvial profile image
Oscar

Great share, very helpful

Collapse
 
uisce profile image
Tony Watters

Thanks Oscar

Collapse
 
ravinleague profile image
Ravi

How do I update an existing navigation node ?

Collapse
 
jaredpittmanmyriad profile image
JaredPittmanMyriad

late to the party, but here's what we use to update links. Basically just a copy/add/delete. Unfortunately, some of these are showing as obsolete now. Seems Pnp wants to make things harder..

Update Notebook to Team OneNote in nav

$OneNoteURL = (Get-PnPNavigationNode | Where-Object title -eq "Notebook" | select url).url
if($OneNoteURL -ne ""){
Add-PnPNavigationNode -Location QuickLaunch -Title "Team OneNote" -Url $OneNoteURL
Remove-PnPNavigationNode -Location QuickLaunch -Title "Notebook" -Force
}

Some comments may only be visible to logged-in visitors. Sign in to view all comments.