3

I got a custom Sharepoint list which has the following list items

Hostname  -   IP   -     OS     -    CPUs - RAM -    Disk

Server      1.1.1.1   Server 2003     3      8    Disk C: 20gb

If i then change, lets say, the RAM so there is now 12gb (or CPU or any of the other values) i would like it to update it)

I got scripts that pull all the information from the servers via WMI and exporting it to a .csv

The csv layout looks like the following

IP,Hostname,OS,CPU,Ram,Disk
1.1.1.1,Server,Server 2003,3,12,Disk C: 20gb

Now here's the real question, i got a script that add's the items to the list, but if i run it again it adds the item to the list so its now there twice, (one with the old value and one with the new) but i need a script that can update the diffrent column values if they should change, without having to remove the item and add it again with the new values.

migrated from stackoverflow.com Dec 18 '11 at 13:34

This question came from our site for professional and enthusiast programmers.

  • You need to fire an update operation. However you should have an ID or something that helps you in uniquely identifying your record. – Nitin Rastogi Dec 8 '11 at 17:52
9

Update list item using powershell -

$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.

$SPItem.Update()

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

Stop-SPAssignment $SPAssignment

Source and more info here: http://mysharepointwork.blogspot.com/2010/09/addupdatedelete-list-items-using.html

  • feel free to ask if you have questions regarding the example – LMW-HH Dec 8 '11 at 15:44
  • @Linus, How do we change the List item ID using powershell? – Nate Oct 26 '16 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.