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