I had the need to publish all files in specific folders for a customer and I had to put several PowerShell’s together to make one that works. What this PowerShell will do is take every document in a specific folder that you specify via the variables and then it will publish any file that has never had a major version. This can be important for migrations.

#Set config variables
$WebURL="Site Name Goes here"
$ListName ="Documents"
$FolderPath = "Shared Documents/Test"

#Get Web and Objects
$Web = Get-SPWeb $WebURL
$List = $Web.Lists[$ListName]

#Query to batch process
$oQuery = New-Object Microsoft.SharePoint.SPQuery
$oQuery.ViewAttributes = "Scope='RecursiveAll'"      
$oQuery.Query = "<Where><Contains><FieldRef Name='FileDirRef'/><Value Type='Lookup'>$FolderPath</Value></Contains></Where>";
$oQuery.RowLimit = "100"

Do {
    #Get List Items defined by the Query
    $ListItems = $List.GetItems($oQuery)
    $oQuery.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition

    #Loop through each list Item
    ForEach($Item in $ListItems)
    {

    #Write-Host "Processing List Item:" $Item.Id, $Item.file
    #$Item.File.Checkin("Checked in by Administrator",[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
    if ($Item.Level -ne "Published"){
            $HasPublished = $Item.HasPublishedVersion
            #write-host $HasPublished
            if ($HasPublished -eq $true){
                #Write-Host $Item.file already has a major version Skipping -ForegroundColor Green
                }else{
                #$Item.File.Publish("Published Using Powershell")
                #$Item.File.Update()
                Write-Host $Item.file "Now has a major version" -ForegroundColor Green
                $Name = $Item.Name
                Write-Output "$Name Now has a major version" >> D:\PublishLog.csv
            }
        }
    }
} While ($oQuery.ListItemCollectionPosition -ne $Null)

Leave a Reply