SharePoint Online: Perform Bulk Operations (Add/Update/Delete) Faster in Batches with New-PnPBatch cmdlet in PowerShell

The new PnP PowerShell Module brings the ability to perform bulk operations in batches in SharePoint Online using the New-PnPBatch cmdlet. This is extremely helpful when dealing with bigger lists with create, edit or delete operations. The PnP Batch operations are accelerated and relatively reduce execution time and deliver faster performance. So now, You can perform such operations in bulk rather than execute each command individually.

SharePoint Online: Perform Bulk Operations (Add/Update/Delete) Faster with New-PnPBatch PowerShell cmdlet

Add SharePoint Online List Items in Bulk:

I tried inserting 1000 items into a list, and it took just 25 seconds with the new PnP PowerShell’s batch feature. Whereas in a usual Add-PnPListItem method, it took 250 seconds to add 1000 items to the list. Here is an example:

$SiteURL = "https://Crescent.sharepoint.com/sites/Operations"
$ListName = "Random"

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

#Create a New Batch
$Batch = New-PnPBatch

#Add List Items
For($i=1; $i-le 1000; $i++)
{
    Add-PnPListItem -List $ListName -Values @{"Title" = $i} -Batch $Batch
}

#Send Batch to the server
Invoke-PnPBatch -Batch $Batch

Here, instead of sending each request directly to the SharePoint Online site with Add-PnPListItem cmdlet, we batched them with $Batch variable, and then the batch is sent to SharePoint Online using the Invoke-PnPBatch cmdlet.

Update SharePoint Online List Items in Batches:

Processing time could be a nightmare when dealing with a large amount of data. Fortunately, the PnP PowerShell batch cmdlets reduce the execution time by about 10 times compared with the conventional methods. The batch method not only reduces the execution time but also sends a fewer number of requests to the server so that you can avoid the throttling issue in SharePoint Online.

#Create a New Batch
$Batch = New-PnPBatch

#Update List Items
For($i=1; $i-le 1000; $i++)
{    
    Set-PnPListItem -List $ListName -Identity $i -Values @{"Title" = "Updated Item:"+$i} -Batch $Batch
}

#Send Batch to the server
Invoke-PnPBatch -Batch $Batch

How to Bulk Delete List Items in Batches?

It took around ~50 seconds to delete 1000 items from a list. It is worth noting that not all cmdlets support batch operation today, but I suppose the support for batching will be added soon on relevant cmdlets.

$SiteURL = "https://Crescent.sharepoint.com/sites/Operations"
$ListName = "Random"

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

#Get List Items to Delete
$ListItems =  Get-PnPListItem -List $ListName -PageSize 500

#Create a New Batch
$Batch = New-PnPBatch

#Delete All List Items
ForEach($Item in $ListItems)
{    
     Remove-PnPListItem -List $ListName -Identity $Item.ID -Batch $Batch
}

#Send Batch to the server
Invoke-PnPBatch -Batch $Batch

If you want to create a large number of dummy files for testing purposes, use: How to bulk create files in SharePoint Online using PowerShell?

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