Introduction
Welcome readers to an article on updating list items in SharePoint 2013 and Office 365 using PowerShell.
Here we will see how to update all the list items in a list when a requirement of such comes in.
I have a PowerShell script here that you just need to add using Notepad and save it as a .ps1 file. In this script I am updating a choice field from a look up field.
Script
- Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
- $webURL = "Your Site Url"
- $listName = "Name of your list"
- $web = Get-SPWeb $webURL
- $list = $web.Lists[$listName]
- $items = $list.items
- Foreach($item in $items)
- {
- $prim = $item["Look Up Column field"]
- if(!$prim)
- {
- write-host "Null"
- }
- else
- {
- $trim = $prim.split('#')[1]
- write-host $trim
- $item["choice field"] = $trim
- $item.Update()
- write-host $item["Your Result You want to see"]
- }
- }
How to run
- You need to change the fields Site Url, List Name and Fields.
- You might have a question, why am I using a split function?
The field value when fetched from a look up field has characters like “1,#”. Using split we eliminate that extra data by giving us a correct variable.
- Run the SharePoint PowerShell as Administrator
- Run the .ps1 file
It will be executed and you will see the updated field values.
Here is a time saver article for my readers. Keep learning.
Cheers!