Add,Update,Delete List items using powershell sharepoint 2010

A simple example of a powershell script to add items in a SharePoint 2010 list.

$spAssignment = Start-SPAssignment
$mylist = (Get-SPWeb -identity http://SP -AssignmentCollection $spAssignment).Lists["listName"]
$newItem = $mylist .Items.Add()
$newItem["Title"] = “Added by Powershell”
$newItem["description"] = “My PowerShell Magic”
Stop-SPAssignment $spAssignment

Update list item example I -

$SPAssignment = Start-SPAssignment
$SPWeb = Get-SPWeb http://SP -AssignmentCollection $spAssignment

Next step is to get the list:
$SPList = $SPWeb.Lists["Announcements"]

When we have located the list we can retrieve the item. The quickest way is to use the GetItemByID() method:
$SPItem = $SPList.GetItemById("1")

The example above requires that you know the ID of the item. If you don’t know the items ID you can use the Where-Object cmdlet instead:

$SPItem = $SPList.Items | Where { $_["Title"] -eq "New Announcement" }
When you’ve retrieved the item you can modify the item information.

$SPItem[“Title”] = "MyTitle"
$SPItem[“Body”] = "MyBody"

After modifying an item you have to call the Update() method to set the changes.

When you’re done, use the Stop-SPAssignment cmdlet to dispose the SPWeb object. Stop-SPAssignment $SPAssignment

Delete Item -

The example below will count the items and loop down, read the name of the item, and if the item contains a 3, then it will delete that item.

$site = new-object Microsoft.SharePoint.SPSite("") # is a legit url
$relweburl = '/Docs"
$web = $site.openweb($relweburl)
$list=$web.Lists["testList"] $listItems = $list.Items
$listItemsTotal = $listItems.Count

for ($x=$listItemsTotal-1;$x -ge 0; $x--)
Write-Host("DELETED: " + $listItems[$x].name)


  1. This comment has been removed by the author.

  2. Is there any ohter way to delete the list item in sharepoint list?(means with out using powershell)



This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at .

Copyright (c) 2010 @ All rights are reserved.Do Not Copy.

@ Learning