SharePoint Online: Bulk Add/Update/Delete Faster with New-PnPBatch in PowerShell

The 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 larger 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’s worth mentioning 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

Getting “New-PnPBatch not recognized” error? Well, update your PowerShell module! 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 - 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!

5 thoughts on “SharePoint Online: Bulk Add/Update/Delete Faster with New-PnPBatch in PowerShell

Leave a Reply

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