Skip to main content

Bulk Import Excel Data to SharePoint List Using PowerShell and PnP

 

Managing large datasets in SharePoint can be tricky, especially when you're dealing with Excel files and need to avoid list view threshold issues. In this guide, I’ll walk you through a PowerShell script that efficiently imports data from Excel into a SharePoint Online list using PnP PowerShell — with batching support for performance.

Prerequisites

Make sure you have the following before running the script:

  • SharePoint Online site URL

  • Excel file with data properly formatted

  • PnP PowerShell module installed (Install-Module PnP.PowerShell)

  • Appropriate SharePoint permissions

What the Script Does

  • Connects to your SharePoint site

  • Loads and reads an Excel file

  • Converts Excel date values

  • Batches records in groups (to avoid the 5000 item threshold)

  • Adds the items to your SharePoint list or library

  • Logs execution time

PowerShell Script

$siteUrl = "[Site Collection URL]"
Connect-PnPOnline -Url $siteUrl -UseWebLogin

# Capture the start time
$startTime = Get-Date

# Define lists and field mappings
$LibraryName = "[LibraryName]"
$excelFilePath = "[excelFilePathwithExtention]" 

# Import data from the Excel file using Import-Excel
$excelData = Import-Excel -Path $excelFilePath

# Define the batch size (to avoid SharePoint's list view threshold of 5000 items)
$batchSize = 200

# Initialize a list to hold batches of items
$batch = @()

# Loop through each row in the Excel data
foreach ($row in $excelData) {
    # Calculate the OBSERVATION_DATE
    $baseDate = [datetime]"1900-01-01"
    $convertedDate = $baseDate.AddDays($row.OBSERVATION_DATE - 2)

    # Map Excel data to SharePoint fields
    $Values = @{
        "Title"                     = $row.Title
        "SHIFT"                     = $row.SHIFT
        "OBSERVATION_DATE"         = $convertedDate
        "BEHAVIOR"                 = $row.BEHAVIOR
        "ROLE"                      = $row.ROLE
        "Behaviour_unsafeCondition" = if ($row.AT_RISK_BY_UNSAFE_COND -eq "N") { "No" } 
                                      elseif ($row.AT_RISK_BY_UNSAFE_COND -eq "Y") { "Yes" } 
                                      else { "" }
        "LOCATION_ID"               = $row.LOCATION_ID
        "AREA_ID"                   = $row.AREA_ID
        "SUB_AREA_ID"               = $row.SUB_AREA_ID
        "LINE_ID"                   = if ($null -eq $row.LINE_ID -or $row.LINE_ID -eq "") { $null } else { $row.LINE_ID }
        "CATEGORY_ID"               = $row.CATEGORY_ID
        "SUB_CATEGORY_ID"           = $row.SUB_CATEGORY_ID
    }

    $batch += $Values

    # Process the batch if it reaches the threshold
    if ($batch.Count -ge $batchSize) {
        foreach ($item in $batch) {
            Add-PnPListItem -List $LibraryName -Values $item -Verbose
        }
        Write-Host "Added $($batch.Count) items to $LibraryName"
        $batch.Clear()
    }
}

# Add remaining items if any
if ($batch.Count -gt 0) {
    foreach ($item in $batch) {
        Add-PnPListItem -List $LibraryName -Values $item -Verbose
    }
    Write-Host "Added the last batch of $($batch.Count) items to $LibraryName"
}

# Calculate execution time
$endTime = Get-Date
$executionTime = $endTime - $startTime
Write-Host "Script executed in:" $executionTime

Key Benefits
  • Efficient batching: Avoids the 5000-item limit by processing in chunks.

  • Dynamic date conversion: Transforms Excel numeric dates into valid DateTime objects.

  • Safe and robust: Skips null values and handles conditional logic cleanly.


Comments

Popular posts from this blog

Get App Expiry Dates using Powershell

Step 1: Connect-MsolService Step 2: $applist = Get-MsolServicePrincipal -all  |Where-Object -FilterScript { ($_.DisplayName -notlike "*Microsoft*") -and ($_.DisplayName -notlike "autohost*") -and  ($_.ServicePrincipalNames -notlike "*localhost*") } Step 3: foreach ($appentry in $applist) {     $principalId = $appentry.AppPrincipalId     $principalName = $appentry.DisplayName     Get-MsolServicePrincipalCredential -AppPrincipalId $principalId -ReturnKeyValues $false | ? { $_.Type -eq "Password" } | % { "$principalName;$principalId;" + $_.KeyId.ToString() +";" + $_.StartDate.ToString() + ";" + $_.EndDate.ToString() } | out-file -FilePath d:\appsec.txt -append }

๐Ÿš€ Essential SPFx (SharePoint Framework) Commands for Every Developer

Whether you're new to SharePoint Framework (SPFx) or a seasoned pro, having a go-to list of core commands can significantly improve your development workflow. In this post, we'll walk through the most important SPFx CLI commands—from setting up your environment to packaging and deploying your solution. ๐Ÿ› ️ Setting Up Your SPFx Project Start by scaffolding a new project using the Yeoman generator provided by Microsoft. Make sure Node.js and npm are installed before you proceed. yo @microsoft/sharepoint After scaffolding the project, install all dependencies: npm install To update dependencies later: npm update ๐Ÿ” Trusting the Development Certificate If you're using the local workbench, you need to trust the developer certificate for HTTPS support: gulp trust-dev-cert ๐Ÿงช Running the Local Workbench To build and serve your project locally, use: gulp serve This command starts a local server at: https://localhost:4321/temp/workbench.html You can also test your solution in ShareP...