Activate a feature on all My Sites

Script

Name

Description

Last Modified Date

Download

ActivateMySiteFeature.ps1

Automatically locates the MySite web application and activates a feature on it.

5/12/2012

None

#Script: Activate MySite Feature
#https://codemonkeysoftware.atlassian.net
#Description: This script locates the my site web application and activates a feature.
#Version: 5/12/2012
$featureId = "AF62E54D-388E-48A8-B0A6-D886CF6A9AF2" #Update this value or convert to parameter.

cls
$mysiteHostTemplate = "SPSMSITEHOST"
$webApps = Get-SPWebApplication
$mysiteWebApp = $null;


 
#Look for the my site web application. If there are more than 1, then ask.  
foreach($webApp in $webApps)
{
	$sites = Get-SPSite -Limit All -WebApplication $webApp | where-object { $_.RootWeb.WebTemplate -eq $mysiteHostTemplate }
	if($sites -ne $null -and $sites -is "Array" -and $sites.Count -eq 1)
	{
		$mysiteWebApp = $webApp
		break;
	}
	if($sites -ne $null -and $sites -is "Microsoft.SharePoint.SPSite")
	{
		$mysiteWebApp = $webApp
		break;
	}
}
if($mysiteWebApp -eq $null)
{
	$mysiteWebApp = Get-SPWebApplication (Read-Host "Please enter the url of the My Site Web Application");
}
if($mysiteWebApp -eq $null)
{
	throw "Cannot locate my site web app. Terminating."
}
Write-Host -ForegroundColor Green ("Found My Site Web Application at Url: " +  $mysiteWebApp.Url)
foreach($site in $mysiteWebApp.Sites)
{
	try
	{
		if($site.Features[$featureId] -ne $null)
		{
			Write-Host ("Feature already active on site " + $site.Url);
			continue;
		}
		Enable-SPFeature -Identity $featureId -Url $site.Url -Confirm:$false -ErrorAction:Stop
		Write-Host ("Activated feature on site at " + $site.Url)
	}
	catch
	{
		Write-Warning ("Failed to activate feature with ID " + $featureId + " on site " + $site.Url + " with error " + $_)
	}
}

CodeMonkey Software is a division of JCHMedia www.jchmedia.com