4

I want to update the user of a people or group field. The property allow multiple users to set for this field. How can I keep the users who are already present in this field and update the existing user using Powershell script?

For e.g I have user1,user2,user3 in the people or group field. Now I want to update user1 to user4.

How can I achieve this?

Any help would be appreciated

Thanks.

3 Answers 3

5

You will need to read the column value first and then add new values then update.

Get Multi-valued People Picker Field Value (if Allow multiple selections enabled!) using PowerShell:

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Variables
        $SiteURL = "<replace site url>"
        $ListName = "Tasks"               
        $FieldName="Assigned To"


    #Get site and List objects
    $web = Get-SPWeb $SiteURL
    $list= $web.lists[$listName]

    #Iterate through each row in the list
    foreach ($Item in $list.Items)
    {
        if($item[$FieldName])
        {
            #Get People picker field values collection
            $UserCollection = New-Object Microsoft.Sharepoint.SPFieldUserValueCollection($Web,$item[$FieldName].ToString())
            Write-host Item ID: $item["ID"]

            #Get each User from the Person or Group field
            foreach($User in $UserCollection)
            {
                 #Get the Display Name of the user
                 Write-host $User.LookupValue 
                 #Get the Login Name (Account ID)
                 Write-host $User.User
                 #Get the E-mail
                 Write-host $User.User.Email             
            }
        }
    }

Update Multiple Values People Picker Field using PowerShell script

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Variables
$SiteURL = "http://portal.crescent.com/sites/Sales"
$ListName = "Tasks"               
$FieldName="Assigned To"

#Get site and List objects
$web = Get-SPWeb $SiteURL
$list= $web.lists[$listName]

#Get the List Item to update
$ListItem  = $List.GetItemByID(2)

#User Account to set
$UserAccounts="Crescent\Salaudeen; Crescent\Ravi"
$UserAccountsColl = $UserAccounts -split ';'

$UserCollection = new-object Microsoft.SharePoint.SPFieldUserValueCollection
foreach($UserAccount in $UserAccountsColl)
{
    #Get the User
    $User=$web.EnsureUser($UserAccount)

    #Add to collection
    $UserFieldValue = new-object Microsoft.SharePoint.SPFieldUserValue($Web, $User.ID, $User.LoginName)
    $UserCollection.Add($UserFieldValue)
}

#update the Multiple value Person or Group field
$ListItem[$FieldName] = $UserCollection
$ListItem.Update()

Source: http://www.sharepointdiary.com/2015/12/get-set-person-or-group-people-picker-field-values-using-powershell.html

2
  • This works fine for single user but for multiple user I am getting this error Exception calling "Update" with "0" argument(s): "Invalid look-up value. A look-up field contains invalid data. Please check the value and try again." Please refer this link: 3d-grphique.blogspot.in/2015/03/… I am facing the same problem. Oct 19, 2016 at 5:03
  • It looks like you have copied at least the most of this answer from the following blog post: sharepointdiary.com/2015/12/…. When using someone's work, it's always important to mention the original author/source. I have edited your answer to give an example how this could be done in this case.
    – moe
    Oct 26, 2016 at 7:07
3

I have write code, It is not the full code, but may be it will help you to move forward

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
$webUrl = "http://localSite"
$library = "Sports" 
$s = new-object Microsoft.SharePoint.SPSite($webUrl)    
$w = $s.OpenWeb()         
$l = $w.Lists[$library] 

Write-Host   "Connecting to site" $webUrl 
foreach ($listItem in $l.Items)
 {
  #Get Multiple Users from list column 
  $MultipleUsers=$listItem["Users"] 

  #Remove the User 1 from above variable, write some code for this 

  #then add users one by one in below 

  [Microsoft.SharePoint.SPFieldUserValueCollection]$lotsofpeople = New-Object Microsoft.SharePoint.SPFieldUserValueCollection            


  $user1 = $w.EnsureUser("User2");            
  $user1Value = New-Object Microsoft.SharePoint.SPFieldUserValue($w,     
  $user1.Id, $user1.LoginName) 
  $lotsofpeople.Add($user1Value); 

  $user2 = $w.EnsureUser("User3");            
  $user1Value = New-Object Microsoft.SharePoint.SPFieldUserValue($w,   
  $user2.Id, $user2.LoginName) 
  $lotsofpeople.Add($user2Value); 

  $user3 = $w.EnsureUser("User4");            
  $user1Value = New-Object Microsoft.SharePoint.SPFieldUserValue($w,  
  $user3.Id, $user3.LoginName) 
  $lotsofpeople.Add($user3Value); 

  #then update current Item Users with new values 

  $listItem["Users"]= lotsofpeople
}
0

Make sure you are defining the the user Collection as below

   [Microsoft.SharePoint.SPFieldUserValueCollection]$ItemSPuserList = new-object Microsoft.SharePoint.SPFieldUserValueCollection

Your Answer

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

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