Working with Managed Paths in SharePoint using PowerShell

Quite a common requirement for implementations of SharePoint that I am involved in is to create new managed paths for a given web application.

While it is a simple task to perform via Central Administration, I inevitably turn to PowerShell to achieve this so that I can then include it as part of larger configuration or deployment scripts.

Define managed paths in Central Administration
Define managed paths in Central Administration

To manage managed paths in SharePoint we use the PowerShell Get-SPManagedPathNew-SPManagedPath and Remove-SPManagedPath cmdlets.

Reviewing existing managed paths

To get a list of all the managed paths for a given web application we use the Get-SPManagedPath cmdlet as shown below.

Get-SPManagedPaths PowerShell Cmdlet
Get-SPManagedPaths PowerShell Cmdlet

Creating a new explicit path

Explicit managed paths only allow one site collection to be created at a specific path. An example of this is the root site collection of a web application which has an explicit managed path of “/” (https://sharepoint.jcallaghan.com).

To add a new explicit managed path to a web application we use the New-SPManagedPath cmdlet and include the -Explicit parameter.

Adding a wildcard managed path

Wildcard managed paths allow one or more site collections to exist at a specified path. This is the same as the default ‘sites’ managed path that we should all be familiar with (https://sharepoint.jcallaghan.com/sites/projectxyz).

To add a wildcard managed path we run the command as we did for an explicit managed path however we don’t include the -Explicit parameter.

Custom managed paths added
Custom managed paths added

Removing an existing managed path

There may be times when you need to remove managed paths. This can be done by running the Remove-SPManagedPath cmdlet and specifying the name of the managed path to be removed and what web application to remove it from. When removing a managed path you will be prompted to confirm the removal action – this can be silenced by adding -confirm:$false to the command.

Conclusion

Using the SPManagedPath nouns in PowerShell we can get a list of existing managed paths, create explicit or wildcard managed paths and also remove existing managed paths for a given web application.

Adding a Yes, No, Cancel prompt to a PowerShell script

When I write PowerShell scripts, I often want to step through sections. This allows me to decided whether to proceed with parts of my script or not. I achieve this by using the ChoiceDescription class as demonstrated below.

More information about using prompts in PowerShell can be found in the following article and tip on Microsoft TechNet.

Clear the SharePoint Quick Launch using PowerShell

Today I had a requirement to remove all the headings and links from the quick launch navigation of hundreds of SharePoint sites. The sites were being provisioned as part of a PowerShell deployment script that was deleting the default list and libraries. Going through each of these sites manually was not an option – so I edited the deployment script to include a function to remove the headings for me.

SharePoint Quick Launch with Headings
SharePoint Quick Launch with Headings

I remembered doing something similar to this back on SharePoint 2007 but I didn’t have access to the previous script or project and instead had to research the subject for a while to find what I needed.

Solution

A post from Get-SPScripts supplied me with what I was after, although it was part of a much larger script. So I picked away at their code and made it into the following PowerShell function to re-use in other projects.

The above Remove-SPQuickLaunchLinks function will remove all headings and links from the SharePoint quick launch for a particular site.

Empty SharePoint Quick Launch
Empty SharePoint Quick Launch

Remember to review, rename and test this script before using it in a production environment.

Review, rename and test

Review

It goes without saying…but whenever you download and use any code or script from this site, or any site for that matter, you should review and understand what the code or script is doing before you attempt to use it.

Rename

Using the PowerShell scripts that I share as an example. I distribute these scripts as text files as it provides an opportunity to review the script, after which you can knowingly rename the file. This also prevents any hastily or accidental execution of the script.

Test

And as with anything you download from the internet – you should test it in a dedicated environment prior to any production environment.