SharePoint Online: Update List Items using PowerShell
Requirement: SharePoint Online PowerShell to Edit List Item.
Update List Items in SharePoint Online using PowerShell CSOM Script
If you want to update a list item in SharePoint Online, but don’t want to go through the web user interface every time – as updates happen frequently, and are time-consuming, There is a way to automate this process with PowerShell. This blog post will show you how to update a list item in SharePoint Online using PowerShell.
Here is the example for SharePoint Online PowerShell to update a list item:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/"
$ListName="Projects"
$UserName="[email protected]"
$Password ="Password goes here"
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = $credentials
try{
#Filter and Get the List Items using CAML
$List = $Context.web.Lists.GetByTitle($ListName)
#Get List Item by ID
$ListItem = $List.GetItemById(1)
#Update List Item title
$ListItem["Title"] = "Project Darwin"
$ListItem.Update()
$Context.ExecuteQuery()
write-host "Item Updated!" -foregroundcolor Green
}
catch{
write-host "$($_.Exception.Message)" -foregroundcolor red
}
This updates the Item with ID: 1 for the given list. You can also use $ListItem.SystemUpdate() method to preserve metadata like the last modified and keep versions intact.
The above script updates a particular list item. What if you want to update all items on the list?
PowerShell to Update All List Items in SharePoint Online:
List items in SharePoint Online can be updated using PowerShell. This can be done by retrieving the list items and then updating their properties. Here is the PowerShell to update list items in SharePoint Online:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Variables
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
Try {
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
$Web = $Ctx.web
#Get the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#Get All List items
$ListItemsCAML = New-Object Microsoft.SharePoint.Client.CamlQuery
$ListItemsCAML.ViewXml = "<View Scope='RecursiveAll'></View>"
$ListItems = $List.GetItems($ListItemsCAML)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
Write-host "Total Items Found:"$List.ItemCount
#Iterate through each item and update
Foreach ($ListItem in $ListItems)
{
#Set New value for List column
$ListItem["Reference"] = $ListItem["ID"]
$ListItem.Update()
$Ctx.ExecuteQuery()
}
Write-host "All Items in the List: $ListName Updated Successfully!" -ForegroundColor Green
}
Catch {
write-host -f Red "Error Updating List Items!" $_.Exception.Message
}
SharePoint Online: PnP PowerShell to Update List Item
PowerShell is a powerful scripting language that can be used to automate a variety of tasks, including updates to SharePoint list items. In order to update a list item, you must first connect to your SharePoint site using PowerShell. Once you are connected, you can use the Get-PnPListItem cmdlet to retrieve the list item you wish to update. To update the item, simply use the Set-PnPListItem cmdlet, specifying the ID of the list item and the new values for the fields you wish to update. This cmdlet will automatically save your changes back to SharePoint.
Here is how to update list items using PnP PowerShell in SharePoint Online using the Set-PnPListItem cmdlet.
#Config Variables
$SiteURL = "https://crescent.sharepoint.com"
$ListName ="Projects"
$ListItemID ="10"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get List Item to Update
$ListItem = Get-PnPListItem -List $ListName -Id $ListItemID -ErrorAction Stop
#Update List Item - Internal Names of the columns : Value
Set-PnPListItem -List $ListName -Identity $ListItem -Values @{"ProjectName" = "SharePoint 2016 Migration"; "ProjectID"="Abj-IT-3021"}
Similarly, we can use the query parameter to filter and get a list item:
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Projects"
$ListItemTitle ="SharePoint 2016 Migration"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get List Item to Update by Query
$ListItem = Get-PnPListItem -List $ListName -Query "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>$ListItemTitle</Value></Eq></Where></Query></View>" -PageSize 1 -ErrorAction Stop
#Update List Item - Internal Names of the columns : Value
Set-PnPListItem -List $ListName -Identity $ListItem -Values @{"Title" = "SharePoint 2016 Migration V2"; "ProjectID"="Abj-IT-3025"}
If you want to bulk update list items from a CSV file, use: SharePoint Online: Update List Items from a CSV File using PowerShell
How to Update Fields like Lookup, People Picker, Managed metadata, Hyperlink, etc.?
The above scripts work just fine with simple field types like Single lines of text, choice, Yes/No, etc. However, we have to handle them differently for other field types such as Managed metadata, Hyperlink, Lookup, People Picker, etc. Here are my posts to help:
- SharePoint Online: PowerShell to Update Lookup Field Value
- SharePoint Online: Update Hyperlink Field Value using PowerShell
- SharePoint Online: PowerShell to Update Lookup Field Value
- SharePoint Online: Update Managed Metadata Field Value using PowerShell
- SharePoint Online: Update Person or Group Field Values using PowerShell
- SharePoint Online: Update Choice Field Value using PowerShell
- SharePoint Online: Update “Yes/No (Checkbox)” Field Value using PowerShell
- SharePoint Online: PowerShell to Update Date Field Value
I have been trying to access/manipulate a SharePoint Online list through PowerShell, and while I can connect to the site, and populate $ctx, it’s failing on $Ctx.Web.Lists. The error I’m getting is “An error occurred while enumerating through a collection: The collection has not been initialized…”
Any thoughts?
Use:
$Lists = $Ctx.Web.Lists
$Ctx.Load($lists)
$CtxExecuteQuery()
Hi,
I want to update file meta data without changing mobified by / modified date in CSOM using powershell. Can anyone please help me??
Just store the current value of “modified by/ Modified Date” field values in a variable and then update them at end of your update. Here is how: How to update Created By / Modified At Metadata Values in SharePoint Online
Hi Salaudeen – I have a script that adds a an item list and it works. Are you able to help me attach an attachment when I create a new item?
I have a list item that will always contain an attachment and would like to create a list item and attach a file at the same time. Any idea how I can incorporate attaching a file in my script?
Sure, Here is how to add attachment to SharePoint Online List Item: SharePoint Online: How to Add Attachment to List Item using PowerShell
How i can delete one Item ?
Here is how to delete list items using PowerShell How to Delete List Items in SharePoint Online using PowerShell
can you show an example where you update the SP list if the ID in the csv is equal to the ID on the SP list?
Sure, You can use this post: Update SharePoint List Items from CSV File using PowerShell
#Config Variables
$SiteURL = “https://xxxxxxx.sharepoint.com/sites/test”
$ListName =”test_powershell”
$data_file = Import-Csv -Path “D:\csv\test_powershell.csv” -Delimiter “,”
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
#Get List Item to Update
$ListItems = Get-PnPListItem -List $ListName
foreach($file in $data_file)
{
foreach($ListItem in $ListItems)
{
$serverURL = $ListItem[“siteurl”]
if($serverURL -eq $($file.Url))
{
$itemID = $ListItem[“ID”]
$itemDes = $ListItem[“comments”]
if($itemDes)
{
Write-Host “Comments available”-f Green
#Get List Item to Update
$ListItemID = Get-PnPListItem -List $ListName -Id $itemID -ErrorAction Stop
#Write-Host $ListItemID[“comments”] -f Green
#Write-Host “”
$itemDes = $itemDes + “;” + $file.Description
Set-PnPListItem -List $ListName -Identity $ListItemID -Values @{“comments” = $itemDes}
#-Values @{“ProjectName” = “SharePoint 2016 Migration”; “ProjectID”=”Abj-IT-3021”}
$itemDes = $null
}
else
{
Write-Host “Nocomments ” -f Red
#Get List Item to Update
$ListItem = Get-PnPListItem -List $ListName -Id $itemID -ErrorAction Stop
$itemDes =$file.Description
Set-PnPListItem -List $ListName -Identity $ListItem -Values @{“comments” = $itemDes}
$itemDes = $null
#-Values @{“ProjectName” = “SharePoint 2016 Migration”; “ProjectID”=”Abj-IT-3021”}
}
}
}
}