This script is intended to recursively apply a custom master page to all the sites created in a site collection. The script uses the Get-SPSite cmdlet to get access to the  specific site collection and the AllWebs property in the SPSite object as the way to obtain all the sites in the site collection. For each site, the MasterUrl property is updated to use the custom master page already deployed to the master page gallery.

 

PowerShell
Editar script|Remove
############################################################################################################################################ 
#This script allows to apply a custom master page recursively to all the sites defined in a site collection 
#Required parametes: 
#   ->$spSite: Site Collection Url 
#   ->$sMasterUrl: Marte Page file 
############################################################################################################################################ 
If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )  
{ Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell } 
 
$host.Runspace.ThreadOptions = "ReuseThread" 
 
#Definition of the function that applies the custom master page to all the sites in a site collection 
function Apply-MasterPageToAllSites 
{    
    try 
    {  
        $spSite = Get-SPSite $sSiteUrl     
        $spsubWebs = $spSite.AllWebs     
        foreach($spsubWeb in $spsubWebs) 
        { 
            Write-Host "Applying custom master page to ($($spsubWeb.Url))" -foregroundcolor green 
            $spsubWeb.MasterUrl= $sMasterUrl 
            $spsubWeb.Update() 
        }      
        $spSite.Dispose() 
    } 
    catch [System.Exception] 
    { 
        write-host -f red $_.Exception.ToString() 
    } 
} 
 
Start-SPAssignment –Global 
#Desaplicamos la página maestra 
$sSiteUrl="http://<SiteCollectionUrl>" 
$sMasterUrl="/_catalogs/masterpage/custom.master" 
Apply-MasterPageToAllSites 
Stop-SPAssignment –Global 
 
Remove-PsSnapin Microsoft.SharePoint.PowerShell