How to modify an item's value in a SharePoint

2020-04-11 06:56发布

问题:

How do I use PowerShell to modify an item's value in a SharePoint list? When I try the following:

$splist.GetItems() | ForEach-Object{
 #Write-Host $_["Item"]

 if($_["Item"] -eq $null){
  $SPFileCollection = $folder.Files
  $upFile=Get-ChildItem D:\Tools\SP-scripts\MDNSO-template.aspx
  $tmpValue = $_["Title"]
  $filename = "EM_Doc_Library\$tmpValue"
  $SPFileCollection.Add("EM_Doc_Library\$tmpValue",$upFile.OpenRead(),$false)
  Write-Host "creating the file"
  Write-Host $_["Title"]
  $_["Item"] = "MDNSO-<$tmpValue>"
 }
}

It says, unable to index into an object of type SPlistItem.

回答1:

Here is a summary of what you could try to do:

$spWeb = Get-SPWeb -Identity http://yourdomain/sites/config
$spList = $spWeb.Lists["AppSettings"]
$spItem = $spList.GetItemById(10013) //or another way that you prefer.
$spItem["Name"] = "MyName"
$spItem.Update()

For more info you should check out this blog post. It contains examples of how to add, update and delete list items with PowerShell: http://mysharepointwork.blogspot.no/2010/09/addupdatedelete-list-items-using.html



回答2:

Using powershell I did it very perfectly with the script from social.msdn.microsoft.com/Forums

Here is sample code, Tyepe "YES" to proceed once prompted. and Replace the only two variable: (urlToTheYourSite & YourListNameToDelete)

write-host "This will delete data, type YES to continue"
$retval = read-host 
if ($retval -ne "YES") 
{
    write-host "exiting - you did not type yes" -foregroundcolor green
    exit
}
write-host "continuing"

$web = get-spweb https://urlToTheYourSite
$list = $web.lists | where {$_.title -eq "YourListNameToDelete"}
Write-host "List $($list.title) has $($list.items.count) entries"
$items = $list.items

foreach ($item in $items)

{
    Write-host "  Say Goodbye to $($item.id)" -foregroundcolor red

    $list.getitembyid($Item.id).Delete()
}