Add-Remove User to SharePoint Multi-Valued People Picker Field using PowerShell

Requirement: 
We have a SharePoint list called “Projects” with 1000s of items. The list has a field called “Team Members” which allows multiple user values. We often get a requirement to either add or remove a particular user to all items or specific items filtered by other columns in the list.

Solution:

Adding-removing users from multiple items in bulk can be achieved with PowerShell. Let’s use PowerShell to add or remove users from the SharePoint people picker (person or group) field value.

powershell to add remove user to people picker column value in sharepoint

PowerShell to Add new user to Person or Group Field:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Configuration parameters
$SiteURL = "https://portal.crescent.com/projects/"
$ListName = "Projects"                
$FieldName= "TeamMembers"
$UserToAdd="Crescent\Sam"

#Get site and List objects
$web = Get-SPWeb $SiteURL
$List = $web.Lists.TryGetList($ListName)

#Item Ids to update
$IDColl= @(4,5,7,14,55,169,258,260,261)

#Iterate through each item
foreach($ItemID in $IDColl)
{
    #Get the Item
    $Item = $List.GetItembyID($ItemID)
    Write-Host "Processing: "$item["ProjectName"]
    
    #Get Existing field value
    $MultiUserCollection = [Microsoft.SharePoint.SPFieldUserValueCollection]$item[$FieldName]
          
    #Prepre the user to Add
    $User = $Web.EnsureUser($UserToAdd)

    #Add new user to the collection
    $NewUser = new-object Microsoft.SharePoint.SPFieldUserValue($Web, $User.ID,$User.Name)
    $MultiUserCollection.Add($NewUser)

    #Update the field value
    $item[$FieldName] = $MultiUserCollection
    $item.update()   
 
    write-host "Team Member Added!"     
}

PowerShell to remove user from Multi-user People Picker field:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Configuration parameters
$SiteURL = "https://portal.crescent.com/projects/"
$ListName = "Projects"                
$FieldName= "TeamMembers"
$UserToRemove="Crescent\Sam"

#Get site and List objects
$web = Get-SPWeb $SiteURL
$List = $web.Lists.TryGetList($ListName)

$IDColl= @(184,259,281,282,306,318,331,378,404,410)
foreach($ItemID in $IDColl)
{
        #Get the Item
        $Item = $List.GetItembyID($ItemID)
        Write-Host "Processing: "$item["ProjectName"]
        
        #Get Existing field value
        $MultiUserCollection = [Microsoft.SharePoint.SPFieldUserValueCollection]$item[$FieldName]
        $NewUserCollection = new-object Microsoft.SharePoint.SPFieldUserValueCollection
       
        #Prepre the user to remove
        $User = $Web.EnsureUser($UserToRemove)
  
        #Create a new collection - exclude user to remove      
        Foreach($MultiUser in $MultiUserCollection)
        {
            if($MultiUser.User.LoginName -ne $User.LoginName)
            {
                #Add user to new collection
                $NewUser = new-object Microsoft.SharePoint.SPFieldUserValue($Web, $MultiUser.User.ID,$MultiUser.User.Name)
                $NewUserCollection.Add($NewUser)
            }
         }
        #Update the list item 
        $item[$FieldName] = $NewUserCollection
        $item.update()   
  
        write-host "User Removed From Existing People Picker field Value!"     
 }

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *