Monday, October 19, 2015

Publish Everything in the Master Page Gallery with Powershell

Publishing you fickle fiend! Have you ever worked in the master page gallery and made edits to files, but when you ask other users to validate your work and they can't see it? It’s probably because the files aren't published for everyone to see. Instead of going through the UI and thinking you published all the files, here's a surefire way to publish every file in the master page gallery.

Sure you could turn off publishing in the master page gallery, but Powershell is a little bit sexier. Plus this script will tell you every file it's publishing. This script will not publish files that are checked out. Instead it'll kindly tell you that it can't publish that file because it's checked out. If you need to check in a bunch of files, you can do that with SharePoint Designer.

Just use this Powershell script. When you run it, provide the URL for your site collection.

Param ([string]$webUrl)

# load the SP snapin if it's not already loaded

if((Get-PSSnapin Microsoft.Sharepoint.Powershell -ErrorAction SilentlyContinue) -eq $null)


    Add-PSSnapin Microsoft.SharePoint.Powershell;


$web = get-spweb $webUrl

# publish the files for the design package

function PublishFilesInFolder($folder) {

    $folder.Files | ?{ $_.Item -ne $null -and $_.Item.Properties["HtmlDesignLockedFile"] -eq $null -and $_.MinorVersion -ne 0 } | %{

        Write-Host ("Publishing {0}" -f $_.Url);

        $_.Publish("design package deployment");


    $folder.SubFolders | %{ PublishFilesInFolder $_; }


PublishFilesInFolder $web.GetFolder("_catalogs/masterpage")

Credit goes to Jonathan Rupp for writing this script two years ago. I recently ran into this scenario again and scoured my inbox to find this script to help save both the day and my sanity.