locked
Powershell so all of subsites/sub-sub sites inherit from Parent site's master page RRS feed

  • Question

  • Hi there,

    I use SP 2010.

    I have a single web application with thousands of sites/subsites and so on. All of these should have master pages inheriting from the parent site which is http://localhost

    If you could give me or point me to a PowerShell script to do this, that will be greatly appreciated.

    Thanks in advance.

    Monday, November 12, 2012 10:19 PM

Answers

  • Start-SPAssignment -Global
    $site = Get-SPSite http://localhost/
    $topWeb = Get-SPWeb $site.Url
    $site | Get-SPWeb -limit all | ForEach-Object {if (($_.WebTemplate -ne "SRCHCEN") -and ($_.WebTemplate -ne "SRCHCENTERLITE") -and ($_.WebTemplate -ne "SRCHCENTERFAST")) {$_.CustomMasterUrl = $topWeb.CustomMasterUrl;$_.AllProperties["__InheritsCustomMasterUrl"] = "True";$_.MasterUrl = $topWeb.MasterUrl;$_.AllProperties["__InheritsMasterUrl"] = "True";$_.AlternateCssUrl = $topWeb.AlternateCssUrl;$_.AllProperties["__InheritsAlternateCssUrl"] = "True";$_.Update();}}
    Stop-SPAssignment -Global
    Above script worked nicely, thanks folks.

    • Marked as answer by ran009 Wednesday, November 14, 2012 5:48 PM
    Wednesday, November 14, 2012 5:48 PM

All replies

  • The following PowerShell will audit a web and all of the sub-webs, and produce a report containing the master pages used.

    function Update-MasterPage($SiteUrl,$CheckSubWebs)
    {
    $items = New-Object psobject
    $items | Add-Member -MemberType NoteProperty -Name "Title" -value ""
    $items | Add-Member -MemberType NoteProperty -Name "MasterPage" -value ""
    $items | Add-Member -MemberType NoteProperty -Name "CustomMasterPage" -value ""
    $items | Add-Member -MemberType NoteProperty -Name "Web" -value ""
    $a = $null
    $a = @()
    $w = get-spweb $SiteUrl;
    $b = $items | Select-Object *; 
    $b.Title = $w.Title;
    $b.Web=$w.Url;
    $b.CustomMasterPage= $w.CustomMasterUrl;
    $b.MasterPage =  $w.MasterUrl;
    $a+=$b;
    
    if($CheckSubWebs)
    {
    if($w.Webs.Count -gt 0)
    {
    foreach($sw in $w.Webs)
    {
    $a += Update-MasterPage -SiteUrl $sw.Url -CheckSubWebs $true
    }
    } 
    }
    $w.Dispose();
    Write-Output $a
    }
    $a = $null
    $a = @()
    $a = Update-MasterPage -SiteUrl "http://sneakpreview" -CheckSubWebs $true
    $a | FT Title,MasterPage,CustomMasterPage
    $a | Where-Object {$_} | Export-Csv -Delimiter "," -Path C:\export.csv -notype

    To update the page being used, then use this (slight variation):

    function Update-MasterPage($SiteUrl,$CheckSubWebs)
    {
    $items = New-Object psobject
    $items | Add-Member -MemberType NoteProperty -Name "Title" -value ""
    $items | Add-Member -MemberType NoteProperty -Name "MasterPage" -value ""
    $items | Add-Member -MemberType NoteProperty -Name "CustomMasterPage" -value ""
    $items | Add-Member -MemberType NoteProperty -Name "Web" -value ""
    $a = $null
    $a = @()
    $w = get-spweb $SiteUrl;
    $b = $items | Select-Object *; 
    $b.Title = $w.Title;
    $b.Web=$w.Url;
    $b.CustomMasterPage= $w.CustomMasterUrl;
    $b.MasterPage =  $w.MasterUrl;
    $a+=$b;
    $update = $false;
    if($w.CustomMasterUrl -ne "/_catalogs/masterpage/v4.master")
    {
    $w.CustomMasterUrl = "/_catalogs/masterpage/v4.master"
    $update = $true;
    }
    if($w.MasterUrl -ne "/_catalogs/masterpage/v4.master")
    {
    $w.MasterUrl = "/_catalogs/masterpage/v4.master"
    $update = $true;
    }
    if($update)
    {
    $w.Update();
    }
    
    
    if($CheckSubWebs)
    {
    if($w.Webs.Count -gt 0)
    {
    foreach($sw in $w.Webs)
    {
    $a += Update-MasterPage -SiteUrl $sw.Url -CheckSubWebs $true
    }
    } 
    }
    $w.Dispose();
    Write-Output $a
    }
    $a = $null
    $a = @()
    $a = Update-MasterPage -SiteUrl "http://sneakpreview" -CheckSubWebs $true
    $a | FT Title,MasterPage,CustomMasterPage
    $a | Where-Object {$_} | Export-Csv -Delimiter "," -Path C:\export.csv -notype


    Regards, Matthew
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    Monday, November 12, 2012 11:03 PM
  • Great script Matthew!

    I used a shorter one in the past:

    Get-SPWebApplication http://yourwebapp.com | Get-SPSite -Limit All | Get-SPWeb -Limit ALL | foreach-object{
        Write-Host "Web" $_.Url ": Master page is" $_.MasterUrl "Custom master is" $_.CustomMasterUrl
    }

    This one is for "reporting" and the following is for updating:

    Get-SPWebApplication http://yourwebapp.com | Get-SPSite -Limit All | Get-SPWeb -Limit ALL | foreach-object{
        $_.MasterUrl = "/_catalogs/masterpage/masterurl.master"
        $_.CustomMasterUrl = "/_catalogs/masterpage/mypage.master"
        $_.Update()
    }

    Of course you have to check the master page url carefully.

    Cheers
    Riccardo


    Italian SharePoint Community - My blog - My profile

    Tuesday, November 13, 2012 11:45 AM
  • Start-SPAssignment -Global
    $site = Get-SPSite http://localhost/
    $topWeb = Get-SPWeb $site.Url
    $site | Get-SPWeb -limit all | ForEach-Object {if (($_.WebTemplate -ne "SRCHCEN") -and ($_.WebTemplate -ne "SRCHCENTERLITE") -and ($_.WebTemplate -ne "SRCHCENTERFAST")) {$_.CustomMasterUrl = $topWeb.CustomMasterUrl;$_.AllProperties["__InheritsCustomMasterUrl"] = "True";$_.MasterUrl = $topWeb.MasterUrl;$_.AllProperties["__InheritsMasterUrl"] = "True";$_.AlternateCssUrl = $topWeb.AlternateCssUrl;$_.AllProperties["__InheritsAlternateCssUrl"] = "True";$_.Update();}}
    Stop-SPAssignment -Global
    Above script worked nicely, thanks folks.

    • Marked as answer by ran009 Wednesday, November 14, 2012 5:48 PM
    Wednesday, November 14, 2012 5:48 PM
  • It worked great! 
    I love this almost one-liner, but would like to see it broken down into multiple lines for a more common man. :D 

    Just for readability and so I can understand PowerShell a little better. Anyone?

    Friday, March 2, 2018 5:50 PM
  • 4th line Broken down for readability: 

    Start-SPAssignment -Global $site = Get-SPSite http://localhost/ $topWeb = Get-SPWeb $site.Url $site | Get-SPWeb -limit all | ForEach-Object {
    if (($_.WebTemplate -ne "SRCHCEN") -and ($_.WebTemplate -ne "SRCHCENTERLITE") -and ($_.WebTemplate -ne "SRCHCENTERFAST")) {
    $_.CustomMasterUrl = $topWeb.CustomMasterUrl;
    $_.AllProperties["__InheritsCustomMasterUrl"] = "True";
    $_.MasterUrl = $topWeb.MasterUrl;
    $_.AllProperties["__InheritsMasterUrl"] = "True";
    $_.AlternateCssUrl = $topWeb.AlternateCssUrl;
    $_.AllProperties["__InheritsAlternateCssUrl"] = "True";
    $_.Update();
    }
    }

    Stop-SPAssignment -Global




    Tuesday, March 6, 2018 10:07 PM
  • Hello Joe,

    Quick question: will this script work for SharePoint Online?

    I've made a simple change to the Master Page and have a handful of subsites that are not picking it up. The few steps I know of in Site Settings are not getting it done. Was thinking that a PowerShell script would be needed to make this happen.

    Thanks in advance...

    Thursday, March 8, 2018 9:36 PM
  • Hello Joe,

    Quick question: will this script work for SharePoint Online?

    I've made a simple change to the Master Page and have a handful of subsites that are not picking it up. The few steps I know of in Site Settings are not getting it done. Was thinking that a PowerShell script would be needed to make this happen.

    Thanks in advance...

    Sorry, I don't know.  I'm working with SharePoint 2013.  Anyone else?
    Tuesday, April 17, 2018 10:49 PM