Thursday 5 February 2015

How to update a SharePoint list column through Powershell

PowerShell Script for updating SharePoint List Column

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
 Add-PSSnapin Microsoft.SharePoint.PowerShell
}
 
#Script settings
 

$web = Get-SPWeb https://sitename:port/

$list = $web.Lists["ListName"]

foreach ($item in $list.Items)
{
$item["Column1"] = "Text";
$item["Column2"] = "Text";
$item.Update();
}

write-host "Success"
 #Dispose web
 
$web.Dispose()

If it is a large List we can update the List column batch wise. Below is the script for updating share point list item batch wise

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)

{
  Add-PSSnapin Microsoft.SharePoint.PowerShell


}
 
#Script settings
 
 

$web = Get-SPWeb https://sitename:port/

$list = $web.Lists["Tasks"]

$spQuery = New-Object Microsoft.SharePoint.SPQuery

$spQuery.ViewAttributes = "Scope='Recursive'";

$spQuery.RowLimit = 100

$caml = '<OrderBy Override="TRUE"><FieldRef Name="ID"/></OrderBy>'

$spQuery.Query = $caml

do

{
$listItems = $list.GetItems($spQuery)

$spQuery.ListItemCollectionPosition = $listItems.ListItemCollectionPosition

foreach($item in $listItems)

{
$item["Column1"] = "Text";

$item["Column2"] = "Text";
$item.Update();

}

}
while ($spQuery.ListItemCollectionPosition -ne $null)

write-host "Success"
#Dispose web
$web.Dispose()

 

Hope this helps..