Replace users in lists accross an entire site collection in SharePoint 2016.

Smith88 21 Reputation points
2021-02-10T17:12:42.957+00:00

We have some users in our SP 2016 environment That have bad accounts in list field items. I have wrote some powershell code that will look for a user and replace them, but only if they are being displayed in the list using show field = "Name(with presence)" . It does this by looking for a string with the "Display Name"

Anyways, this doesn't work well if they people field is being displayed as just and "ID" or something.

I have been looking into the User Information List. Can I replace the user there and the site collection follow suite for all the instances of that user in all subsites in site collection?

Basically I just want a way to replace a users bad account in all list item fields in the entire site collection, no matter how they are displayed in the list using powershell.

This seems like it should be a simple task, but it's looking more complicated by the minute.

Thanks in advance for any advice you can provide.

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,624 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,576 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jerryzy 10,571 Reputation points
    2021-02-11T07:16:33.603+00:00

    Hi @Smith88 ,

    Here are some suggestion based on the query in the question.

    1. User Information is a SharePoint system list, it will add user automatically once the user is resolved in any People Picker. It's not supported to update the user object directly in this list.
    2. To update person field in SharePoint List Items, it needs to get the User object firstly using $web.EnsureUser and then update, please refer the following script: Update Single Value Person field:
          Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue  
      
          $SiteURL = "http://sp2016/sites/dev"  
          $ListName = "Employee"                 
          $FieldName="MyPerson"  
      
      
          $web = Get-SPWeb $SiteURL  
          $list= $web.lists[$listName]  
      
          $ListItem  = $List.GetItemByID(3)  
      
      
          $UserAccount="contoso2016\Jerry"  
          #Get the User  
          $User=$web.EnsureUser($UserAccount)  
          $UserFieldValue = new-object Microsoft.SharePoint.SPFieldUserValue($web, $User.ID, $User.LoginName)  
      
      
          $ListItem[$FieldName] = $UserFieldValue  
          $ListItem.Update()  
      

    66805-snipaste-2021-02-11-15-02-12.png

    Update Multiple value Person field:

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue  
    
    $SiteURL = "http://sp2016/sites/dev"  
    $ListName = "Employee"                 
    $FieldName="MulPerson"  
    
    
    $web = Get-SPWeb $SiteURL  
    $list= $web.lists[$listName]  
    
    
    $ListItem  = $List.GetItemByID(3)  
    
    
    $UserAccounts="Contoso2016\Jerry;Contoso2016\bk"  
    $UserAccountsColl = $UserAccounts -split ';'  
    
    [Microsoft.SharePoint.SPFieldUserValueCollection] $UserCollection = new-object Microsoft.SharePoint.SPFieldUserValueCollection  
    foreach($UserAccount in $UserAccountsColl)  
    {  
    
        $User=$web.EnsureUser($UserAccount)  
    
    
        $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()  
    

    66871-snipaste-2021-02-11-15-22-36.png

    Reference:

    Get-Set Person or Group (People Picker) Field Value using PowerShell in SharePoint

    Thanks
    Best Regards


    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.