Fix SharePoint Quick Launch Links using PowerShell

A lot of times when I’m working on an environment for a client, I come across a situation where I’m in need of a way to quickly update links – usually because people have entered them as an absolute URL (e.g. http://intranet/pages/somepage.aspx) instead of a relative URL (e.g. /pages/somepage.aspx).

Usually when I run across a problem like this, I actually get excited – because these are great opportunities to use our buddy PowerShell! Unfortunately, there aren’t many examples out there of how to use PowerShell to manipulate SharePoint Navigation objects. Hopefully this blog post will be helpful for someone looking to do the same thing I needed to do…

Here’s how it all comes together – and if you don’t want to understand how it works just skip to the end and copy/paste the code :).

Getting the Web Object

The first thing we have to do in order to even begin to work on SharePoint Navigation is to retrieve an SPWeb object. To do this, it’s pretty simple – just run the following code, which will store the Microsoft.SharePoint.SPWeb into the $SPWeb variable:

$SPWeb = Get-SPWeb $Web

Getting the Web.Navigation.QuickLaunch Object

After we’ve retrieved that object, if you use Get-Member to view the Methods and Properties, you’ll quickly notice that the Navigation Property has a QuickLaunch property within it. This is exactly what we want. We can now start to use the ForEach-Object cmdlet to actually show us everything beneath the SPWeb.Navigation.QuickLaunch. Or more specifically, we can use an IF statement to make sure we only grab the appropriate URLs – that is, only ones matching the string we want to replace. Here’s the code snippet:

Pretty cool, so essentially we just say if the URL matches the string we’re looking for, then go ahead and tell us it’s working on it – then update it, replacing the value of the FindString variable with the value of the ReplaceString variable. Both of these variables are actually mandatory parameters, you’ll see that part at the end… Finally, run the Update() method on each one to actually commit the changes to the server.

All done right? No, we can’t forget about the children!

Fixing the Child links

So far the code we’ve seen will only touch the parent links, but each link also has a Children node. We can simply use the same code as before, except this time we’ll run it against the $_.Children property of each link node.

And now, if we put it all together, you’ll see that we have a nice, clean function to do a quick find and replace of navigation links on the quick launch of a single SharePoint Web. I’ve called the function Repair-SPLeftNavigation, and it has 3 required parameters – Web, FindString and ReplaceString:

8 thoughts on “Fix SharePoint Quick Launch Links using PowerShell”

My situation would likely be helped by a script very similar to this one. In my case, I am trying to replace a URL ‘/site/subsite’ with a URL ‘/new_parent/site/subsite.’ However, some of the links are already correct, so it seems like this script would potentially break the good links as they also contain the $findString content.

How would I modify this script to find strings that begin with $FindString, rather than contain the string? Thanks!

If I wanted to add a link that isn’t there currently what would the command for that be?

I have a script that I’m going to use to create sub sites. I was hoping to add to it something similar to what you posted to add a link in the left navigation on the parent site. Awesome post by the way.

Since the function targets an individual web, you should be able to do something like this to target an entire web app:
Get-SPWebApplication http://whateverUrl | Get-SPSite -Limit All | Get-SPWeb -Limit All | ForEach-Object { Repair-SPLeftNavigation -Web $_.Url }