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/"
Next, we will get your sign in credentials with Get-Credential
$cred = Get-Credential
In the next line of code, we will put our site page names with in an array called $sites
$sites = "volvo", "ford", "opel", "lada",
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) {
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
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
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'
}
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'
}
Hope this helps any aspiring PowerShell ninjas out there :)
Top comments (4)
Great share, very helpful
Thanks Oscar
How do I update an existing navigation node ?
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.