Updating Group or Person field in a SharePoint 2013 custom list using a Powershell script

The PS script below loops through an SP 2013 custom list and update the Person or Group field.

—————————-

#Updating the SP2013 list item on the Group field.
Add-PSSnapin Microsoft.SharePoint.PowerShell

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 
$site = new-object Microsoft.SharePoint.SPSite("<Site URL>") 
$relweburl = '/<relative web url>'
$web = $site.openweb($relweburl)
$list=$web.Lists["<list name>"]
$listItems = $list.Items
$listItemsTotal = $listItems.Count

 if($listItemsTotal -ne 0)
  {
    Write-Host("Number of Items found : "+$listItemsTotal )
    foreach($item in $listItems)
     { 
      $currentTitle = $item["<field name>"];
      $newApprover = $web.EnsureUser("<new username>")
      Write-Host("Current approver : "+$currentTitle )
 
       if($currentTitle -like "*<comparing username value>*") 
        { 
           Write-Host("Updated, "+$currentTitle+" with "+$newApprover)
           $item["Approver"] = $newApprover;
           $item.Update()
        } 
     }
  }
else
 {
  Write-Host("Nothing Found")
 }

In the script above, update the following placeholders:

  1. Site URL
  2. Relative Web URL
  3. List Name
  4. Field Name
  5. New Username
  6. Comparing username value

After editing the script with your values, save the script using (.ps1) extension and type the following line of code in Powershell console to execute the script.

* <path>\scriptname.ps1 

Hit enter to execute.

NB: Execute the script in your dev or test environment first before you execute it in your prod environment.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s