Updating List Items using PnP PowerShell

Loading

Updating SharePoint Online list items is a crucial task when maintaining data consistency. PnP PowerShell provides a powerful way to modify list items efficiently.

In this guide, you will learn:
How to connect to SharePoint Online
How to update single and multiple list items
How to update specific fields based on conditions


Prerequisites

Before proceeding, ensure that:
PnP PowerShell is installed
You have Edit or Full Control permissions on the SharePoint list
You know 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 updating items, connect to SharePoint Online:

# 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: Update a Single List Item

To update a single item, you must specify the item ID and the fields to modify.

# Update a single item in the SharePoint list
Set-PnPListItem -List "Employee Directory" -Identity 5 -Values @{"Department"="HR"; "JoiningDate"="2024-02-15"}

🔹 The Identity parameter refers to the item ID
🔹 The Values parameter contains the fields to update

Item updated successfully!


Step 4: Update Multiple List Items

If you need to update multiple items, use a loop:

# Get all items where Department is "Sales" and update them to "Marketing"
$items = Get-PnPListItem -List "Employee Directory" | Where-Object { $_["Department"] -eq "Sales" }

foreach ($item in $items) {
Set-PnPListItem -List "Employee Directory" -Identity $item.Id -Values @{"Department"="Marketing"}
}

All items updated!


Step 5: Update Items Based on Conditions

To update items that meet a certain condition, use filtering:

# Update all employees who joined before 2023
$oldEmployees = Get-PnPListItem -List "Employee Directory" | Where-Object { $_["JoiningDate"] -lt "2023-01-01" }

foreach ($emp in $oldEmployees) {
Set-PnPListItem -List "Employee Directory" -Identity $emp.Id -Values @{"Status"="Veteran Employee"}
}

Filtered items updated!


Step 6: Updating Lookup & Choice Fields

Updating a Lookup Field

Set-PnPListItem -List "Employee Directory" -Identity 10 -Values @{"Manager"=15}

🔹 Where Manager is a lookup column, and 15 is the lookup ID

Updating a Choice Field

Set-PnPListItem -List "Employee Directory" -Identity 8 -Values @{"EmploymentType"="Full-Time"}

🔹 EmploymentType is a choice field

Lookup & choice fields updated!


Step 7: Updating Items from a CSV File

If you need to update items in bulk from a CSV file, follow this approach.

CSV File (update-employees.csv)

ID,Department,JoiningDate
3,HR,2024-01-10
7,Finance,2024-03-05
12,IT,2024-04-22

PowerShell Script to Update from CSV

# Import CSV data
$employees = Import-Csv "C:\update-employees.csv"

# Update each employee record
foreach ($emp in $employees) {
Set-PnPListItem -List "Employee Directory" -Identity $emp.ID -Values @{
"Department" = $emp.Department
"JoiningDate" = $emp.JoiningDate
}
}

Bulk update completed from CSV!


Step 8: Verify the Updated Items

To check if the updates were applied:

# Get the last 5 modified items
Get-PnPListItem -List "Employee Directory" -PageSize 5 | Select Title, Department, JoiningDate

Updated items verified!


Common Errors & Troubleshooting

ErrorCauseSolution
Access DeniedInsufficient permissionsEnsure you have Edit or Full Control rights
Cannot find list 'Employee Directory'Incorrect list nameUse Get-PnPList to verify the list name
Field not foundIncorrect column nameUse Get-PnPField -List "Employee Directory" to check column names
Date format issueIncorrect date formatEnsure date values are in "YYYY-MM-DD" format

Leave a Reply

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