1

I have a list with a list item/column 'Category'. Let's say Category has values 'A','B', and 'C'.

I want to find all the values which are 'A' and replace it with 'B'. How should I do it?

I tried the following code but list item is a column. It gives me an error mentioned below code:

ForEach($Item in $ListItems)
{
    $item["A"] = "B";
    $item.Update();
}

The error:

"Column A does not exist. It may have been deleted..."

How can I solve this?

1 Answer 1

1

Your code should be like:

$sourceWebURL = "<Site URL>"
$sourceListName = "<List Name>"

$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]
$spSourceItems = $spSourceList.Items | where {$_['Category'] -eq "A"}

$spSourceItems | ForEach-Object {
    $_['Category'] = "B"
    $_.update()
}

Reference: How to update list item on conditional basis using PowerShell in SharePoint 2010


Or based on your code, you can modify it like:

ForEach($item in $ListItems)
{
    if($item["Category"] -eq "A") 
    {
         $item["Category"] = "B";
         $item.Update();
    }

}
1
  • 1
    Thanks a lot!!! Commented Dec 31, 2020 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.