Updating a SharePoint List using PnP PowerShell

Loading

In SharePoint Online, lists are essential for storing and managing data. Once a list is created, you may need to update its settings, modify fields (columns), or update list items. PnP PowerShell provides efficient commands to update a SharePoint list programmatically.

This guide covers:
How to connect to SharePoint Online
How to update list properties
How to update list columns (fields)
How to update list items


Prerequisites

Before updating a list, ensure that:
You have SharePoint Admin or Site Owner permissions.
You have PnP PowerShell installed on your system.
You know the SharePoint site URL where the list exists.


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

Step 2: Connect to SharePoint Online

To update a list, first connect to the SharePoint site:

# Connect to the SharePoint Online site
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive

🔹 Replace "yourtenant" with your actual tenant name.
🔹 Replace "yoursite" with the target site name.

For app-based authentication, use:

Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -ClientId "Your-App-Client-ID" -Tenant "yourtenant.onmicrosoft.com" -CertificatePath "Path\To\Certificate.pfx"

Connected successfully!


Step 3: Update List Properties

To update list properties like title, description, or enabling versioning, use the Set-PnPList command.

Example 1: Rename a SharePoint List

# Update the title of the SharePoint list
Set-PnPList -Identity "Old List Name" -Title "New List Name"

Example 2: Update List Description

# Update the description of the SharePoint list
Set-PnPList -Identity "Employee Directory" -Description "List containing employee records"

Example 3: Enable Versioning in a List

# Enable versioning for a SharePoint list
Set-PnPList -Identity "Employee Directory" -EnableVersioning $true

List properties updated successfully!


Step 4: Update List Columns (Fields)

To modify columns, use Set-PnPField.

Example 1: Rename a Column

# Rename an existing field in a SharePoint list
Set-PnPField -List "Employee Directory" -Identity "EmployeeName" -DisplayName "Full Name"

Example 2: Change Column Type (Only for Text to Multiple Lines)

# Change a single-line text field to a multi-line text field
Set-PnPField -List "Employee Directory" -Identity "EmployeeName" -Type Note

Example 3: Make a Column Required

# Make the Employee ID column required
Set-PnPField -List "Employee Directory" -Identity "EmployeeID" -Required $true

Columns updated successfully!


Step 5: Update List Items

To update items in a SharePoint list, use Set-PnPListItem.

Example 1: Update a Single Item

# Update the "Department" of an employee with ID 1
Set-PnPListItem -List "Employee Directory" -Identity 1 -Values @{"Department"="HR"}

Example 2: Update Multiple Fields in an Item

# Update multiple fields in an item
Set-PnPListItem -List "Employee Directory" -Identity 2 -Values @{"Employee Name"="John Doe"; "Department"="IT"; "Active Employee"=$true}

Example 3: Bulk Update Multiple Items

# Bulk update all employees in IT department to HR
$items = Get-PnPListItem -List "Employee Directory" | Where-Object { $_["Department"] -eq "IT" }
foreach ($item in $items) {
Set-PnPListItem -List "Employee Directory" -Identity $item.Id -Values @{"Department"="HR"}
}

List items updated successfully!


Step 6: Verify the Updates

Run these commands to check the updated list and items:

Check Updated List Properties

Get-PnPList -Identity "Employee Directory"

Check Updated Columns

Get-PnPField -List "Employee Directory"

Check Updated List Items

Get-PnPListItem -List "Employee Directory"

Verification complete!


Step 7: Restore a Previous Version (If Needed)

If you enabled versioning, you can restore an older version of a list item.

# Restore previous version of an item
Restore-PnPListItemVersion -List "Employee Directory" -Identity 1 -VersionId 2

Previous version restored successfully!


Common Errors & Troubleshooting

ErrorCauseSolution
Set-PnPList : Access DeniedYou don’t have Site Owner/Admin permissionsEnsure you are a Site Owner or SharePoint Admin
The term 'Set-PnPList' is not recognizedPnP PowerShell is not installedRun Import-Module PnP.PowerShell
Column type is invalidIncorrect field type in Set-PnPFieldUse correct types (Text, Note, Number, etc.)
Cannot connect to SharePoint OnlineAuthentication issueUse -Interactive login or App-based authentication

Leave a Reply

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