Update List Items in SharePoint 2013 and Office 365 Using PowerShell

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

  1. Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue  
  2. $webURL = "Your Site Url"  
  3. $listName = "Name of your list"  
  4. $web = Get-SPWeb $webURL  
  5. $list = $web.Lists[$listName]  
  6. $items = $list.items  
  7. Foreach($item in $items)  
  8. {  
  9.   $prim = $item["Look Up Column field"]  
  10.   if(!$prim)  
  11.   {  
  12.     write-host "Null"  
  13.   }  
  14.   else  
  15.   {  
  16.     $trim = $prim.split('#')[1]  
  17.     write-host $trim  
  18.     $item["choice field"] = $trim  
  19.     $item.Update()  
  20.     write-host $item["Your Result You want to see"]  
  21.   }  
  22. }  

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!

Up Next
    Ebook Download
    View all
    Learn
    View all