Adding list items to a SharePoint Online list is a common task when automating SharePoint operations. PnP PowerShell simplifies this process, allowing users to insert single or multiple items programmatically.
This guide will cover:
How to connect to SharePoint Online
How to add single and multiple list items
How to add items with attachments
Prerequisites
Before proceeding, ensure you have:
PnP PowerShell installed
Admin or Editor permissions on the SharePoint site
The SharePoint site URL and list name
Step 1: Install and Import PnP PowerShell
If you haven’t installed PnP PowerShell, install it using:
Install-Module -Name PnP.PowerShell -Scope CurrentUser -AllowClobber -Force
Then, import the module:
Import-Module PnP.PowerShell
PnP PowerShell is ready!
Step 2: Connect to SharePoint Online
Before adding items, connect to your SharePoint site:
# Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive
🔹 Replace "yourtenant"
with your tenant name
🔹 Replace "yoursite"
with the site name
Connection successful!
Step 3: Add a Single List Item
To add a single list item, use:
# Add a new item to a SharePoint list
Add-PnPListItem -List "Employee Directory" -Values @{"Title"="John Doe"; "Department"="IT"; "JoiningDate"="2024-01-01"}
Item added successfully!
Step 4: Add Multiple List Items
To insert multiple items, use a loop:
# Define multiple items
$items = @(
@{ "Title"="Alice"; "Department"="HR"; "JoiningDate"="2024-02-10" }
@{ "Title"="Bob"; "Department"="Finance"; "JoiningDate"="2024-03-15" }
@{ "Title"="Charlie"; "Department"="Marketing"; "JoiningDate"="2024-04-20" }
)
# Loop through and add each item
foreach ($item in $items) {
Add-PnPListItem -List "Employee Directory" -Values $item
}
Multiple items added!
Step 5: Add Items with Attachments
To add an item with an attachment:
# Add a new list item
$item = Add-PnPListItem -List "Employee Directory" -Values @{"Title"="David"; "Department"="IT"}
# Attach a file to the item
Add-PnPFile -Path "C:\EmployeeDetails.pdf" -List "Employee Directory" -ListItem $item.Id
Item added with an attachment!
Step 6: Verify Added Items
To check if the item was added:
# Get the last 5 items added
Get-PnPListItem -List "Employee Directory" -PageSize 5 | Select Title, Department, JoiningDate
Items verified!
Step 7: Adding Items from a CSV File
If you have a CSV file, you can bulk insert data.
CSV File (employees.csv)
Title,Department,JoiningDate
Emma,Sales,2024-05-10
Liam,IT,2024-06-15
Sophia,HR,2024-07-20
PowerShell Script to Add CSV Data
# Import CSV
$employees = Import-Csv "C:\employees.csv"
# Add each employee to the SharePoint list
foreach ($emp in $employees) {
Add-PnPListItem -List "Employee Directory" -Values @{
"Title" = $emp.Title
"Department" = $emp.Department
"JoiningDate" = $emp.JoiningDate
}
}
CSV data imported successfully!
Common Errors & Troubleshooting
Error | Cause | Solution |
---|---|---|
Access Denied | Insufficient permissions | Ensure you have Edit or Full Control rights |
Cannot find list 'Employee Directory' | List name is incorrect | Use Get-PnPList to verify the list name |
Field not found | Column name is incorrect | Use Get-PnPField -List "Employee Directory" to check column names |
Date format issue | Incorrect date format | Ensure date values are in "YYYY-MM-DD" format |