4

We have over 80 Site Collections in our Web Application is there a way through power shell that i can set the same master page for all the site collections.

  • Is the master page and all associated assets already installed in all site collections? – Dave Wise May 22 '12 at 17:19
  • Yes but as there are some changes going on in master page and we want to have uniqueness for all the site collections and it is very tedious process to go and do the change is each individual site collections master page – Anil May 22 '12 at 17:29
3

I don't have a ready-made PowerShell script but these two should get you going. You can enumerate through all site collections in a web application and then apply the master page to each site collection found. The way that the master page is applied is much better than most you will find on the web because it also excludes Search Centers when it recurses through the site.

3

If you have deployed your master page via a feature similar to this one, is all about activating your feature using something similar (http://msdn.microsoft.com/en-us/library/gg447066.aspx) - this has a WebProvisioned event which simply changes master page as soon as the Feature is activated:

Get-SPWebApplication "<WebApplicationURL>" | Get-SPSite -Limit ALL | 
        ForEach-Object {Enable-SPFeature -identity "MyBrandingFeature" -URL $_.URL}

If you have your master page in place, you need to combine the above with

$web = Get-SPWeb http://sharepoint

$web.CustomMasterUrl = "/_catalogs/masterpage/myCool.master"

$web.MasterUrl = "/_catalogs/masterpage/myCool.master"

$web.Update()
1

You can update Master page of any site using powershell by using following code

$site = Get-SPSite http://sp:2000/sites/Ex15
foreach ($web in $site.AllWebs)
{
    $web.MasterUrl = $site.RootWeb.ServerRelativeUrl +  &quot;/_catalogs/masterpage/oslo.master&quot;;
    $web.CustomMasterUrl = $site.RootWeb.ServerRelativeUrl + &quot;/_catalogs/masterpage/oslo.master&quot;;
    $web.Update();
    Write-Host $web.Url $web.MasterUrl $web.CustomMasterUrl;
}
$site.Dispose()

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    #Get All site collections
    $SiteCollections = Get-SPWebApplication | Get-SPSite -Limit All
    #Loop through all site collections
       foreach($Site in $SiteCollections)
        {
            #Loop throuh all Sub Sites
           foreach($Web in $Site.AllWebs)
           {
        $web.MasterUrl = $site.RootWeb.ServerRelativeUrl +  &quot;/_catalogs/masterpage/oslo.master&quot;;
        $web.CustomMasterUrl = $site.RootWeb.ServerRelativeUrl + &quot;/_catalogs/masterpage/oslo.master&quot;;
        $web.Update();
        Write-Host $web.Url $web.MasterUrl $web.CustomMasterUrl;
           }
        }

Full code and description found on this blog CodePlayAndLearn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.