Powershell :: Apply Master Page to all sub sites under site collection

Leave a Comment
Hi All,

In a following post we will see about a PowerShell Script which is used to Apply Master Page to all it sub sites under Site Collection.


if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0))
{
Add-PSSnapin Microsoft.SharePoint.PowerShell
}

$siteCollUrl = Get-SPSite "http://abc:7902/sites/xyz"
if($siteCollUrl -ne $null -and $siteCollUrl -ne "")
{
$topWeb=Get-SPWeb -identity $siteCollUrl.Url;
#$topWeb = Get-SPWeb $site.Url
Write-Host "Process Started .." -foregroundcolor Green
MasterNew($topWeb);
Write-Host "Process Completed .." -foregroundcolor Green
}


Function MasterNew($web)
{
ApplyMaster($web);
$webs=$web.Webs;
$webs | ForEach-Object{
 MasterNew($_);$_.Dispose();
 }
$web.Dispose();
}

Function ApplyMaster($web)
{
 Write-Host "Applying Master Page to :" $web.url;
 $web.CustomMasterUrl = $topWeb.CustomMasterUrl;
 $web.AllProperties[“__InheritsCustomMasterUrl”] = “True”;
 $web.MasterUrl = $topWeb.MasterUrl;
 $web.AllProperties[“__InheritsMasterUrl”] = “True”;
 $web.AlternateCssUrl = $topWeb.AlternateCssUrl;
 $web.AllProperties[“__InheritsAlternateCssUrl”] = “True”;
 $web.Update();
}

Down Load Link:

https://sites.google.com/site/sharepointkitchenblog/ApplyMaster_2_working.ps1


Thanks..

Related Post

0 comments:

Post a Comment