SharePoint Online: How to Approve Multiple Documents using PowerShell?

Requirement: Approve Multiple Documents in SharePoint Online using PowerShell.

PowerShell is a powerful tool that can help you automate common tasks in SharePoint Online. In this blog post, we will show you how to approve multiple documents at the same time in SharePoint Online using PowerShell. This can be a helpful time-saving tip if you need to approve a large number of documents.

How to Approve Multiple Items in SharePoint Online?

When the content approval is turned ON, each item must be approved to be visible to users. You can approve any item or document in SharePoint Online by right-clicking on the document >> Click on More >> Approve/Reject >> Click on “Approve”.

sharepoint online approve multiple documents

Approving documents in SharePoint Online can be a time-consuming process, especially when you have a large number of documents to review. Fortunately, SharePoint provides a way to approve multiple documents at once, using the bulk approval feature.

To approve multiple items, you can select more than one item and click on the “Approve/Reject” button from the command bar or from the context menu!

bulk approve documents sharepoint online

Approve a SharePoint Online Document using PowerShell:

Let’s approve a single file in SharePoint Online using PowerShell:

#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"
  
#Parameters
$SiteURL = "https://Crescent.sharepoint.com"
$FileRelativeURL ="/Shared Documents/Compliance Process.xlsx"
$ApprovalComments= "Approved by the Admin"

#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 the File and Approve
$File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
$File.Approve($ApprovalComments)
$Ctx.ExecuteQuery()
 
Write-Host -f Green "File Approved Successfully!"

Similarly, to reject a file, use: $File.Deny($Comment). We can use the PowerShell script to approve multiple items in SharePoint Online.

SharePoint Online: Approve Multiple Documents using PowerShell

Let’s approve all pending items in a list or document library using PowerShell:

#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"
  
#Parameters
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="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 the File and Approve
$List = $Ctx.web.Lists.GetByTitle($ListName)

#Query to get all items with status other than "Approved"
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "<View Scope = 'RecursiveAll'><Query><Where><Neq><FieldRef Name='_ModerationStatus' /><Value Type='ModStat'>0</Value></Neq></Where></Query></View>"
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()

#Approve all pending List Items
ForEach($Item in $ListItems)
{
    $Item["_ModerationStatus"] = 0
    $Item.Update()
    $Ctx.ExecuteQuery()
    Write-host -f Green "Approved List Item:" $Item.Id
} 

PnP PowerShell to Approve All Files in a Document Library

To approve all files in a SharePoint Online document library, use this PowerShell script:

#Parameters
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName = "Branding"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get All Files in Draft State
$DraftItems = Get-PnPListItem -List $ListName -PageSize 2000 | Where {$_.FileSystemObjectType -eq "File" -and  $_["_ModerationStatus"] -ne 0}

#Approve Files
$DraftItems | ForEach-Object { 
    Set-PnPListItem -List $ListName -Identity $_ -Values @{"_ModerationStatus"=0;"_ModerationComments"="Approved by Script"} | Out-Null
    Write-host "Approved File:"$_.FieldValues.FileRef
}

Here, the moderation status field has these enumerations:

  • Approved: 0
  • Denied: 1
  • Pending: 2
  • Draft: 3
  • Scheduled: 4

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

One thought on “SharePoint Online: How to Approve Multiple Documents using PowerShell?

  • Just a heads up, this does not work anymore:
    Set-PnPListItem -List $ListName -Identity $_ -Values @{“_ModerationStatus”=0;”_ModerationComments”=”Approved by Script”} | Out-Null

    I had to use the Set-PnPFileCheckedIn cmdlet with the -Approve parameter to get it to work.

    Reply

Leave a Reply

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