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-click on the document >> Click on More >> Approve/Reject >> Click on “Approve”.

sharepoint online approve multiple documents

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 is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply