1

First off my code is this.

foreach ($list in $web.lists)
{​​
    foreach ($field in $list.fields)
    {
        if ($field.type -eq 'User')
        {
            write-host $list.Title
            write-host $field
            write-host ""
        }​​
    }​​
}​​

I can get all the lists for the web, check each lists fields and if the field is a 'User' field (People Picker Value); I can get the list title and the 'User' type field name.

This works fine to find the lists I want to query.

Now I want to query those list fields for a particular user and replace that user with another user.

I can't seem to figure out how to pull back the actual data that is in the column.

1 Answer 1

2

Follow the example bellow, this reuse your script above:

function queryAndUpdateUser($siteUrl, $oldUser, $newUser){
  $web = Get-SPWeb -Identity $siteUrl
  $userToChange = $web.EnsureUser($oldUser)
  $newestUser = $web.EnsureUser($newUser)
   foreach ($list in $web.lists)
   {​​
     foreach ($field in $list.fields)
     {
        if ($field.type -eq 'User')
        {
           foreach ($item in $list.Items){
              if($item[$field.InternalName] == $userToChange){
                $item[$field.InternalName] = $newestUser
                $item.Update()
              }
           }
        }​​
     }​​
   }​​
 }
 queryAndUpdateUser -siteUrl "http://spsiteurl" -oldUser "DOMAIN\oldUser" -newUser "DOMAIN\newUser"

I hope I helped and good luck in your challenge!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.