Powershell script to Approve Masterpages and Pagelayouts



Through this article, it will be easy to approve master pages and pagelayouts that you are going to upload. It will save the time of approving each item.

#Add the Sharepoint powershell Snap In
#Call PowershellSnap in Function
Add-PSSnapin Microsoft.SharePoint.Powershell
Set-ExecutionPolicy RemoteSigned -ErrorAction SilentlyContinue -ErrorVariable excPolicyErr
$ExecutionPolicy = Get-ExecutionPolicy
if($excPolicyErr)
{
Write-Host "$excPolicyErr"
}
Write-Host "My 2010 Powershell Script"
$site = Get-SPSite -Identity "http://sharepointHome:2010"
$SPWeb = Get-SPWeb "http://sharepointHome:2010/"
$List = $SPWeb.Lists["Master Page Gallery"];
for($i=0;$i -le $List.Folders.count;$i++)
{
if(($List.Folders[$i].folder.name -eq "MyCustomMasterPage") -or ($List.Folders[$i].folder.name -eq "MyCustomPageLayouts"))
{
$univadisPagesFolder = $SPWeb.GetFolder($List.Folders[$i].Folder.Url);
$univadisPages = $univadisPagesFolder.Files;
foreach ($univadisPage in $univadisPages)
{
$lstItem = $univadisPage.Item;
if($lstItem.ModerationInformation)
{
if ($lstItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Pending)
{
$strStatus= [Microsoft.SharePoint.SPModerationStatusType]::Approved;
$lstItem.ModerationInformation.Status = $strStatus;
$lstItem.Update();
$univadisPage.Approve("Approved");
$univadisPage.Update();
Write-host "Approved:" $univadisPage.Name

}

}
}

}

}
$List.Update()


Save the file with extension .ps1 and call the file using a bat file.

Up Next
    Ebook Download
    View all
    Learn
    View all