Set the Portal Site Connection settings using PowerShell


I’m sure everyone has used the Portal Site Connection settings in SharePoint to add a top-level portal site URL to the breadcrumb. I use it frequently to make sure that all site collections in a web application have a breadcrumb link to the top level/root site collection; however, until today I hadn’t put any thought into making a PowerShell Function to do this automagically to a single site collection or multiple site collections using PowerShell.

It was VERY easy to convert one of my many PowerShell Functions to accomplish this goal, and I started with something simple like:

$site = Get-SPSite http://intranet
$site.PortalUrl = "http://test"
$site.PortalName = "Test Site"
$site.Dispose()

Pretty simple, and certainly effective for a single site – however, I’m always thinking about bigger and better things. 🙂

By adding some logic, an if/else statement and a few parameters; I turned it into an advanced function which can either be used to set a single site collection or all site collections within a web application. Here’s the whole thing:

function Set-SPPortalSiteUrl {
<#
.Synopsis
	Use Set-SPPortalSiteUrl to set the Portal Site Connection Url using PowerShell.
.Description
	This advanced PowerShell function sets the PortalUrl Property of an SPSite Object or multiple SPSites in a SharePoint Web Application
	specified in the -Url parameter.
.Example
	C:\PS>Set-SPPortalSiteUrl -PortalWebAddress http://intranet -PortalName "Intranet Home" -Url http://intranet/hr
	This example sets the Portal Site Connection to http://intranet, with a name of Intranet Home on the http://intranet/hr Site Collection.
.Example
	C:\PS>Set-SPPortalSiteUrl -PortalWebAddress http://intranet -PortalName "Intranet Home" -Url http://intranet -AllSiteCollections
	This example sets the Portal Site Connection to http://intranet, with a name of Intranet Home on the entire http://intranet Web Application.
.Notes
	This function uses an SPSite Object to set the PortalUrl and PortalName properties of one or more SharePoint Site Collections.
.Link
	http://www.sharepointryan.com
 		http://twitter.com/SharePointRyan
.Inputs
	None
.Outputs
	None
.Parameter PortalWebAddress
	The URL to the Portal Site.
.Parameter PortalName
	The Name or Description of the Portal Site.
.Parameter AllSiteCollections
	A Switch parameter, if set - all sites in the web application will be set with the Portal Site Url and Name provided.
.Parameter Url
	The URL of the Site Collection or Web Application to set the Portal Site.
#>    
	[CmdletBinding()]
	Param(
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
	[string]$PortalWebAddress,
	[Parameter(Mandatory=$true)]
	[string]$PortalName,
	[Parameter(Mandatory=$false)]
	[switch]$AllSiteCollections,
	[Parameter(Mandatory=$true)]
	[string]$Url
	)
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
Start-SPAssignment -Global
	if ($AllSiteCollections) {
	$WebApp = Get-SPWebApplication $Url
	$AllSites = $WebApp | Get-SPSite
		foreach ($site in $AllSites) 
		{
		Write-Host "Setting Portal Web Address to $PortalWebAddress on $($Site.Url)..."
		$site.PortalUrl = $PortalWebAddress
		$site.PortalName = $PortalName
		$site.Dispose()
		}
	} 
	else {
	$AllSites = Get-SPSite -Identity $Url
	Write-Host "Setting Portal Web Address to $PortalWebAddress on $($AllSites.Url)..."
	$AllSites.PortalUrl = $PortalWebAddress
	$AllSites.PortalName = $PortalName
	$AllSites.Dispose()
	}
Stop-SPAssignment -Global
}

Happy PowerShelling!

Advertisements

2 thoughts on “Set the Portal Site Connection settings using PowerShell

  1. Kevin Cassidy

    How do I run the Set-SPPortalSiteUrl ? I copied the code into a text file saved it as Set-SPPortalSiteUrl .ps1 in my working directory (i’m on the sharepoint server). I tried to execute as shown in the example (replacing the parameter values of course) like this: .\Set-SPPortalSiteUrl.ps1 -PortalWebAddress http:……… Nothing happens other than the prompt returns right awat. Any ideas?

    • Ryan Dennis

      Hey Kevin,

      It’s a function, so you will need to dot-source the script file you created:
      . .\set-spportalsiteurl.ps1 (that is dot space dot slash file name)

      After that, the function is stored in memory in your PS session. You can then run it like:
      Set-SPPortalSiteUrl -PortalWebAddress http://intranet -PortalName “Intranet Home” -Url http://intranet/hr

      Replace the parameter values with your own. Also, you can run:
      Get-Help Set-SPPortalSiteUrl to see help and examples.

      Hope this helps!
      RD

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s