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 New-PNPBatch cmdlet. This is extremely helpful when you deal with larger lists with create, edit or delete operations. The PnP Batch operations are accelerated and relatively reduces the execution time and delivers faster performance. So now, You can perform such operations in bulk rather than having to 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 to a list, and it took just 25 seconds with the new PnP PowerShell’s batch feature. Whereas in a normal Add-PnPListItem method, it took 250 seconds to add 1000 items to the list. Here is an example:

$SiteURL = "https://crescentintranet.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 Invoke-PnPBatch cmdlet.

Update SharePoint Online List Items in Batches:

Processing time could be a nightmare when dealing with large amount of data. Fortunately, the PnP PowerShell batch cmdlets reduces the execution time about 10 times compared with the conventional methods. The batch method not only reduces the execution time, but also sends 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. Please note, not all cmdlets supports batch operation as of today, but I suppose the support for batching will be added on relevant cmdlets.

$SiteURL = "https://crescentintranet.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

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