Thursday, April 12, 2012

PowerShell: Update a Field in SharePoint

I needed to update a couple of rows from a specific list in SharePoint. The value was luckily always the same. So I wrote this little script:
cls
Add-PSSnapin Microsoft.SharePoint.Powershell
$web=get-SPWeb "YourWeb"
$web.title
$list=$web.Lists["YourList"]
$list.title
foreach($item in $list.Items)
{
   $item["YourField"] = "YourUpdate"
   $item.update()
}
$web.dispose()
I simply pick the web in which I want to work, search for my list there. In this list, I will pick my field and give it a new updated value. Don't forget to use $item.update() at the end, because otherwise your new value won't get displayed.

3 comments:

  1. i want to use if statement, eg if $item["YourField"] = "YourUpdate" then modify $item["YourField 2"] = "YourUpdate 2" for all the item available in all the list and document libraries available in site collection, so can you help me on that

    ReplyDelete
  2. maybe try this:
    foreach($list in $web.lists)
    {
    foreach($item in $list.Items)
    {
    if($item["YourField"] = "YourUpdate")
    {
    $item["YourField 2"] = "YourUpdate 2"
    $item.update()
    }
    }
    }

    ReplyDelete