What i am trying to acchieve is to write a script that will automatically update all master pages on all site collections in a certain web application.

The only problem i am having is that when i have uploaded the master page to the master page gallery, it is not yet checked in.

I would like yo check in the masterpage from powershell so everyone can see it.

Here is some code to demonstrate, this is not all ofcourse:

$web = $SPSite.OpenWeb();

$MasterPagelist = ($web).Lists |? {$_.Title -eq "Master Page Gallery"}
#Try to upload the master page. 
    $MasterPagelist.RootFolder.Files.Add($MasterPageName, $MasterPageBytes) | out-null; 
    if($debug){Write-Host "Added master page to root folder of master page gallery..." -foregroundcolor Green}
    Write-Host "Error : $_" -foregroundcolor Red

$item = $MasterPagelist.items |? {$_.Name -eq $MasterPageName}

#Set the master page to te wanted item. 
foreach ($website in $SPSite.AllWebs) 
    $website.CustomMasterUrl = "/" + $item.url
    $website.MasterUrl = "/" + $item.url
    if($debug){Write-Host "Set the master page of" $website.Url "to" $MasterPageName -foregroundcolor Green}


after setting it this way, everything works for the user i set it from but when i type:


I get false.

Also no one can view the page except for me.

So my question, how do i check in(Major Version) this file from powershell?

  • Your approch is working and maybe there is a reason doing it this way. can you please give us a reason why your ar not using a sharepoint solution (WSP) to do this? In generall this would be the best practice. Not saying you are doing it wrong. i'm just curious why you went with this approch. Commented Aug 10, 2011 at 14:31
  • In PowerShell you can use $web = Get-SPWeb $url instead of $web = $SPSite.OpenWeb(). Furthermore, you can simply use $web.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::MasterPageCatalog) instead of $MasterPagelist = ($web).Lists |? {$_.Title -eq "Master Page Gallery"}. I have not tested if it is faster, but seems better and is language-indepandent at least.
    – pholpar
    Commented Feb 15, 2018 at 11:04

2 Answers 2


You need to CheckIn and Approve your masterpage file after adding. Just after $item = $MasterPagelist.items |? {$_.Name -eq $MasterPageName} add following lines:

$item.File.CheckIn("CheckIn by PowerShell script")
$item.File.Approve("Approve by PowerShell script")
  • I forgot the .file so this is the answer thank you
    – nldev
    Commented Aug 10, 2011 at 12:29

You could call the CheckIn method on the file you just added.

$MasterPageFile = $MasterPagelist.RootFolder.Files.Add($MasterPageName, $MasterPageBytes)
$MasterPageFile.CheckIn("Checkin by Powershell")
$MasterPageFile.Publish("Published by Powershell")

I haven't tested the code, but that should probably be sufficient.

  • Thanks, the $MasterPageFile does not support the function CheckIn however... It gives an error that the file does not contain the method CheckIn..
    – nldev
    Commented Aug 10, 2011 at 10:51
  • Did you remove the '| out-null' from the line where you Add the masterpage to the Files? Commented Aug 10, 2011 at 11:09
  • No i did not, why do i need to remove the out null? out of curiousity? Because what you said is almost right, but the thin i forgot was the $masterpagefile---> .File.checkin()
    – nldev
    Commented Aug 10, 2011 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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