How to add multiple values in peoplepicker field useing powershell

IT SUPPORT 1 Reputation point
2021-03-11T09:27:54.143+00:00

Hi, I have some issues trying to make a script that takes multiple users from AD group and adds them to a sharepoint 2019 lists Person and group collon, at the end I only get one entry in the sharepoint list (the first user that the AD group has) my script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Configuration Variables

$FieldName="Fieldname"

Get site and List objects

$web = Get-SPWeb https://sitename
$list= $web.GetList("https://sitename/list")

Get the List Item to update

$ListItem = $List.GetItemByID(644)

User Account to set

$UserAccounts=Get-ADGroupMember -Identity "ADgroupname"

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

#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()

Any help would be appreciated

SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,572 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,359 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MichaelHan-MSFT 18,016 Reputation points
    2021-03-12T04:27:21.143+00:00

    Hi @IT SUPPORT ,

    You need to configure the variable $UserCollection as the below:

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

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments