Home > Software engineering >  PowerShell batch
PowerShell batch

Time:02-17

I am reading data from CSV which is having 10000 items and creating on SharePoint list of items. I want to put that in batch using PnP PowerShell. Someone suggests to me batch functionality how to use it?

CodePudding user response:

I'm hoping I understand your question properly and that it is about performing actions in batches of max. 1000 items at a time while the input comes from a much larger CSV file.

If that is what you're asking, you can do like below:

# import the large (10000 items) csv file in a variable
$data = Import-Csv -Path '<your input Csv file>'

# now loop through that in batches of max. 1000 items
$counter = 0
while ($counter -lt $data.Count) {
    # determine the amount of rows to process
    $maxRows = [math]::Min(1000, $data.Count - $counter)
    # loop through the data array using indexing in batches of $maxRows size
    for ($index = 0; $index -lt $maxRows; $index  ) {
        # perform your PnP* action here on each item
        # referenced by $data[$index   $counter]
    }
    # increment the main counter
    $counter  = $maxRows
}
  • Related