Publish multiple pages in SharePoint

As a SharePoint admin most of the time we are getting requirement to publish pages or while working on migration for some cases we would require to publish pages. Here is the small piece of code which can help to publish single page or multiple pages using Powershell.

This piece of code is required for both

$LibraryTitle = "<Replace with Page Library Name>"
$SiteURL = "<Replace with your site URL>"
$oWEb = Get-SPWeb $SiteURL
$PublishingComment ="Published using powershell"

Use below block if you wanted to publish a single page, replace with your page url.

<# Single page publish #>
$pageURL = "https://digiboek.net/sites/demo/SitePages/Sample.aspx"
$file = $oWEb.GetFile($pageURL)
$file.Publish($PublishingComment);

If you wanted to publish multiple pages then use below piece of code

<# Publish all pages from library #>
$oLib = $oWEb.GetFolder($LibraryTitle)
$files = $oLib.Files | select Name, Url
foreach ($file in $files) {
    $ofileURL = $SiteURL + "/" + $file.Url
    $oFile = $oWEb.GetFile($ofileURL)
    if ($oFile.Exists -eq $true) {
        $oFile.Publish($PublishingComment);
        Write-Host $file.Name" Page published successfully..." -foreground Green
    }
}

Happy Coding 😊