Publishing All Items in Document Library using PowerShell Script

During the migration from development server to production server we need to publish all the files. Sometimes we miss some files when we are publishing them manually.

So, in this blog post we are going to see how to use PowerShell script to publish all items in a document library.


  1. Get the object of SPSite
    $site = Get-SPSite $SiteCollectionUrl
  2. Get the object of SPWeb
    $web = $site.RootWeb
  3. Get the object of SPList
    $list = $web.Lists[$LibraryTitle];
  4. Get the object of SPFolder
    $folder = $list.RootFolder;
  5. Next iterate through all the files in folder
    foreach($file in $folder.Files)
  6. Check if files is not published
    if ($file.Level -ne "Published")
  7. Check if files are checked-out then check-in major version
    if ($file.Level -eq "Checkout");
         $file.CheckIn("CheckingIn", [Microsoft.SharePoint.SPCheckinType]::MajorCheckIn);
  8. Otherwise publish the files
    $file.Publish('Published using Powershell');
  9. Then update the files

Attached is the PowerShell script for publishing all the files in a document library. We can use the below script to call the powershell script.

.\ PublishItems.ps1 –SiteCollectionUrl “url” –SiteTitle “title” –LibraryTitle “LibraryName”

Enjoy with this PowerShell script.