none
Update List with Powershell RRS feed

  • Question

  • Hello

    I want to update a list with this command: $item.Update

    Values of One column should be updated. The script does first delete all values and then inserts the new ones.

    Is it possible to only modify the values and not deleting them before. The reason is that some values do not Need to be

    modified and have to stay.

    Thank you

    Nickxx

    Friday, June 26, 2020 1:30 PM

All replies

  • Hi Nickxx,

    As I test ,update method will not delete the values before insert the new value.

    My test code:

    $Web = Get-SPWeb "http://sp"
    $List = $Web.Lists["test3"]
    $ItemID=2
    #Get the List item to update
    $ListItem = $List.GetItembyID($ItemID)  
    #Set Column values
    $ListItem["Title"] = "New Value for the Field!3"
    #Update the Item, so that it gets saved to the list
    $ListItem.Update() 

    Best Regards,

    Amos


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.



    Monday, June 29, 2020 6:14 AM
  • Hi Amos

    Thank you

    Will try it.

    Regards

    Juan

    Monday, June 29, 2020 8:57 AM
  • Hi,

    Would you please provide us with an update on the status of your issue?

    Best Regards,

    Amos


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Friday, July 3, 2020 7:37 AM
  • Hi Amos
    Thank you very much for your answer.

    How do I insert it in a "for each" command.

    foreach($item in $items)
        {        
            $owner = $item['owner_value']
            $ownertest = $owner -replace ".*#"
            $costcentercsv = $CSVData | Where-Object {$_.UsEMail -eq $ownertest} | % UsAccountNum
            $item[$costcenter] = $costcentercsv
            $item.Update();
            
         }
    

    Regards

    Juan

    Monday, July 6, 2020 7:28 AM
  • Hi,

    Is it reporting any error?If you need further,you'd better provide more details for your solution(CSV data).

    Best Regards,

    Amos


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, July 6, 2020 9:45 AM
  • Hi Amos
    Thank you for your answer.
    Your code is intended to run once but I need to embed it into a for each command
    Or am I mistaken.
    Have a great day and Keep safe.
    Nickxx

    Wednesday, July 8, 2020 6:45 AM
  • Hi,

    Your code looks ok.

    My test code(I didn’t process the data, just set a value):

    $ListItems = $List.Items;
    
    foreach($ListItem in $ListItems){
    
    $ListItem["Title"] = "New Value for the Field!3"
    
    $ListItem.Update()
    
    }

    Best regards,

    Amos



    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Proposed as answer by LinyusMVP Thursday, July 9, 2020 8:17 AM
    Wednesday, July 8, 2020 8:21 AM
  • Hi Amos
    Thank you.

    But how is it possible to not delete al values at once.

    I would like to deletete them only at the moment of the change/update.

    What I mean is: Avoid deleting the complete column at the beginning.
    The goal is: Only do a modify and leave the other fields unmodified.

    Regards

    Juan


    • Edited by Nickxx Monday, July 13, 2020 11:36 AM
    Monday, July 13, 2020 11:35 AM