Sunday, March 8, 2015

How to Update List Items in SharePoint using PowerShell

How to Update List Items in SharePoint using PowerShell Script? Well, the basic syntax for updating a list item goes as:

#Get Web and List 
$Web = Get-SPWeb "http://sharepointsite.com"
$List = $Web.Lists["ListName"]

#Get the List item to update
$ListItem = $List.GetItembyID($ItemID)
 
#Set Column values
$ListItem["ColumnName"] = "New Value for the Field!"
 
#Update the Item, so that it gets saved to the list
$ListItem.Update()

Update SharePoint list item using PowerShell script
Lets take an another example:
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue

$web = Get-SPWeb http://SharePointSite.com

#Get the Contacts List
$List = $web.Lists["Contacts"]

#Get the List Item by its Title field value
$ListItem = $List.Items | Where {$_.Title -eq "Salaudeen Rajack"}

#Set "Department" column value
$ListItem["Department"] = "IT"

$ListItem.Update() 

Update All List items using PowerShell script:

$web = Get-SPWeb http://SharePointSite.com

#Get the Contacts List
$List = $web.Lists["Contacts"]

#Get All Items by update its title
$ListItems = $List.items
 
#Iterate through each item
foreach($Item in $ListItems)
{
    #Update the value of the "Title" column
    $item["Title"] = "Title Updated!"
 
    #Update the item
    $item.Update()
}

How about Updating Selective List Items in Bulk:
Lets update list item by its id field.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$SiteURL = "https://portal.crescent.com/pipeline/"
$ListName = "Deals"

#Get the List
$List = (Get-SPWeb $SiteURL).Lists.TryGetList($ListName)

#List of Item IDs to update
$ItemIDs = 16,52,69,70,97,170,194,195,196,220,259If($list)
{
    foreach($ItemID in $ItemIDs)
    {
        #Get Item by ID
        $ListItem = $List.GetItembyID($ItemID) 

        #Update List Item Fields
        $ListItem["Health and SafetyRisk"]="Low"
        $ListItem["Environmental Risk"]="Medium"
        $ListItem["Social Risk"]="High"       

        $ListItem.SystemUpdate()
        Write-host "Updated Item ID:"$ItemID
    }
}



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...