How to publish/approve a page in SharePoint 2010 from powershell

Shahed picture Shahed · Feb 21, 2014 · Viewed 8.3k times · Source

I have a list urls for specific SharePoint 2010 pages. I can visit each page and click the publish button. Then approve button to publish the pages.

I am trying to automate the process. I am wondering if there is any way to do that from powershell?

Answer

Luis picture Luis · Feb 23, 2014

The script bellow should do it:

$web = Get-SPWeb http://demo2010a:20905
$pages = "http://demo2010a:20905/Pages/TvAndRadioAlerts.aspx","http://demo2010a:20905/Pages/Systems.aspx"
$pages | ForEach-Object {
$item = $web.GetListItem($_)
    if ($item.File.CheckOutType -ne "None")
    {
        $item.File.CheckIn("Automatically checked in by Powershell", "MajorCheckIn");
    }
    if ($item.Versions[0].Level -ne "Published")
    {
        $item.File.Publish("Automatically published by Powershell");
    }
    if ($item.ModerationInformation.Status -ne "Approved")
    {
        $item.File.Approve("Automatically approved by by Powershell");
    }
}