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.
Steps:
- Get the object of SPSite
$site = Get-SPSite $SiteCollectionUrl - Get the object of SPWeb
$web = $site.RootWeb - Get the object of SPList
$list = $web.Lists[$LibraryTitle]; - Get the object of SPFolder
$folder = $list.RootFolder; - Next iterate through all the files in folder
foreach($file in $folder.Files) - Check if files is not published
if ($file.Level -ne "Published") - Check if files are checked-out then check-in major version
if ($file.Level -eq "Checkout");
$file.CheckIn("CheckingIn", [Microsoft.SharePoint.SPCheckinType]::MajorCheckIn); - Otherwise publish the files
$file.Publish('Published using Powershell'); - Then update the files
$file.Update();
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.
JaumePosted Sep 16, 2015, 3:59 AM
The script only works for files in the rootfolder. I suggest using foreach ($item in $list.Items) and then $file = $item.File.
P TPosted Aug 8, 2014, 11:42 AM
HI Brijendra. Thanks for the really useful post. We are in the process of migrating thousands of documents into SharePoint 2013. We are using document sets. Unfortunately the powershell script doesn't seem to work with document sets. Any ideas?