SOLVED

Powershell script to update multiple items from ID field

Copper Contributor

Hello,

I have a request for 2 SharePoint 2013 OnPrem list where they are needing 1,000 records in both lists to simply have the status updated to completed and comment added for specific records.  Of course, this would take a lot of hours to manually touch 2,000 records.  I've tried to put together a script that will use ID field and then update the status and comment field but no luck with the test.  Can someone possibly tell me what I might be missing?  Am I using incorrect field names in the script for the default ID? Thanks a ton for any help.

 

 

 

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Variables
$SiteURL = "http://siteaddress/"
$ListName = "vr"
 
#Get the List
$List = (Get-SPWeb $SiteURL).Lists.TryGetList($ListName)
 
#List of Item IDs to update
$ItemIDs = 17148, 17147
 
If($list)
{
    foreach($ItemID in $ItemIDs)
    {
        #Get Item by ID
        $ListItem = $List.GetItembyID($ItemID) 
 
        #Update List Item Fields
        $ListItem["Status"]="Completed" 
        $ListItem["Request Comment"]="Completed 10-21-2022"     
 
        $ListItem.SystemUpdate()
        Write-host "Updated Item ID:"$ItemID
    }
}

 

 

 

18 Replies

@dklbwf you could do this with a simple flow in Power Automate. With 1000 records in each list it woul take a short while to run but is probably a more simple solution.

 

Rob
Los Gallardos
Microsoft Power Automate Community Super User

Thanks for response. This is actually older SharePoint 2013 OnPrem envrionment.

@dklbwf 

Is it all items in those SharePoint list?

in case you can use this:

$web = Get-SPWeb https://yoursite
 
#Get the List
$List = $web.Lists["YourList"]
 
#Get All Items in the list
$ListItems = $List.items
  
#Iterate through each item
foreach($Item in $ListItems)
{
    #Update the value of your "YourField" 
    $item["YourField"] = "Hello!"
  
    #Update the item
    $item.Update()
}

 

Thanks for response. It's not all items, I am needing to update only specific items and wanting to update them by using the ID assigned to each item in the list.
best response confirmed by dklbwf (Copper Contributor)
Solution
@dklbwf try this :) 

 

 

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Variables
$SiteURL = "http://siteaddress/"
$ListName = "vr"

$List = (Get-SPWeb $SiteURL).Lists.TryGetList($ListName)
$ItemIDs = 17148, 17147

    foreach($ItemID in $ItemIDs)
{

    $SPListItem = $List.GetItemByUniqueId($ItemID)
    $ListItem["Status"]="Completed"
    $ListItem["Request Comment"]="Completed 10-21-2022"    
    $SPListItem.Update()
}

 

 

@NicolasKheirallah This is the error I am seeing when I run the script

Ah sorry mate! It should be $List.GetItemById($ItemID) not $List.GetItemByUniqueId($ItemID)

@NicolasKheirallah Thanks for the quick response! After updating it is saying it can't find item even though the ID im using is there. I've tried several different ID's but same error. Thanks again

 

dklbwf_0-1666636265655.png

 

Try this:

$items = $list.GetItems()
foreach ($item in $item) {
Write-Host $item["ID"]
}
And see what item ID is in the output!
Nothing is showing when I use that script. No error or anything. NOt sure if i have it correctly:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$SiteURL = "http://site"
$ListName = "Vendor Requests"

$List = (Get-SPWeb $SiteURL).Lists.TryGetList($ListName)
$items = $list.GetItems()
foreach ($item in $item) {
Write-Host $item["ID"]
}

@dklbwf 

 

foreach ($item in $items) {
Write-Host $item["ID"]
}
You forgot the s in items :lol:
Gotcha. Okay same thing after adding "s". No error at all but nothing is showing after script runs. Thnx
What is you do a
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$SiteURL = "http://site"
$ListName = "Vendor Requests"
What does give you :

$List = (Get-SPWeb $SiteURL).Lists.TryGetList($ListName)
$items = $list.GetItems()
Write-Host $items
I am not seeing any error or anything when I run the script above. It does not give me and output either.

@dklbwf Are there any item level permissions applied on list items? Make sure user account you are using to run PowerShell has at least Contribute/Edit access to read and update items in SharePoint list.


Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

That was 100% the issue. I was running the scripts under the wrong account. Once I figured that out script is working 100% correctly.
I was running the scripts under the wrong account. Once I figured that out script is working 100% correctly. Thanks a ton for your help!
Anytime mate, I was guessing that was the issue :)
1 best response

Accepted Solutions
best response confirmed by dklbwf (Copper Contributor)
Solution
@dklbwf try this :) 

 

 

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Variables
$SiteURL = "http://siteaddress/"
$ListName = "vr"

$List = (Get-SPWeb $SiteURL).Lists.TryGetList($ListName)
$ItemIDs = 17148, 17147

    foreach($ItemID in $ItemIDs)
{

    $SPListItem = $List.GetItemByUniqueId($ItemID)
    $ListItem["Status"]="Completed"
    $ListItem["Request Comment"]="Completed 10-21-2022"    
    $SPListItem.Update()
}

 

 

View solution in original post