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