SharePoint Online: Publish All Files in a Library using PowerShell

Requirement: Bulk Publish Multiple Files in SharePoint Online

PowerShell to Publish All Files in a Library using SharePoint Online

PowerShell to check-in, Approve and Publish All Files in a Library:

When you turn ON minor versioning in the SharePoint Online document library, any edits will not appear to users with read-only (or view only) permissions until you publish them. Publishing each file individually in large libraries is definitely cumbersome, So here is my PowerShell script to publish all of them at once. Say, you want to publish all pages in the “Pages” library in SharePoint Online.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set Parameters
$ListName ="Team Documents"

#Get Credentials to connect
$Cred= Get-Credential

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Get All Items from the List
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 

    ForEach($ListItem in $ListItems)
        #Approve the File if "Content Approval is Turned-ON"
        If ($List.EnableModeration -eq $true)
            If ($ListItem["_ModerationStatus"] -ne '0')
                $ListItem.File.Approve("Approved by Admin")
                Write-Host "File Approved: "$ListItem["FileLeafRef"] -ForegroundColor Yellow

        #Checkin the File if its checked-out
        If ($ListItem["CheckoutUser"] -ne $null)
            $ListItem.File.CheckIn("Check-in by Admin", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
            Write-Host "File Checked in: "$ListItem["FileLeafRef"] -ForegroundColor Cyan

        #Publish the File
        If($List.EnableVersioning -and $List.EnableMinorVersions)
            $ListItem.File.Publish("Published by Admin")
            Write-Host -f Green "File published:" $ListItem["FileLeafRef"]
Catch {
Write-host -f Red "Error:" $_.Exception.Message
You can also use “Content and Structure” to bulk publish documents in SharePoint Online!

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *