kwizcom banner advertisement

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

Peron or Group column in SharePoint captures user values. Here is my collection of PowerShell scripts to work with People picker field values:
Get Person or Group People Picker Field Value using PowerShell in SharePoint


Read People Picker Field Value using PowerShell:
PowerShell script to get value of Person or Group field.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Variables
$SiteURL = "http://portal.crescent.com/sites/Sales"
$ListName = "Tasks"                
$FieldName="Assigned To"
 
#Get site and List objects
$web = Get-SPWeb $SiteURL
$list= $web.lists[$listName]

#Iterate through each row in the list
foreach ($Item in $list.Items)
{
    #Get the People picker field value
    $AssignedTo = New-Object Microsoft.Sharepoint.SPFieldUserValue($Web,$Item[$FieldName])

    #Get the Login Name of the user
    Write-host $AssignedTo.User.LoginName
    #Get the Email
    Write-host $AssignedTo.User.Email
}

Get Multi-valued People Picker Field Value (if Allow multiple selections enabled!) using PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Variables
$SiteURL = "http://portal.crescent.com/sites/Sales"
$ListName = "Tasks"                
$FieldName="Assigned To"
 
#Get site and List objects
$web = Get-SPWeb $SiteURL
$list= $web.lists[$listName]

#Iterate through each row in the list
foreach ($Item in $list.Items)
{
    if($item[$FieldName])
    {
        #Get People picker field values collection
        $UserCollection = New-Object Microsoft.Sharepoint.SPFieldUserValueCollection($Web,$item[$FieldName].ToString())
        Write-host Item ID: $item["ID"]
        
        #Get each User from the Person or Group field
        foreach($User in $UserCollection)
        {
             #Get the Display Name of the user
             Write-host $User.LookupValue 
             #Get the Login Name (Account ID)
             Write-host $User.User
             #Get the E-mail
             Write-host $User.User.Email             
        }
    }
}

PowerShell script to Update Person or Group Field:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Variables
$SiteURL = "http://portal.crescent.com/sites/Sales"
$ListName = "Tasks"                
$FieldName="Assigned To"
 
#Get site and List objects
$web = Get-SPWeb $SiteURL
$list= $web.lists[$listName]

#Get the List Item to update
$ListItem  = $List.GetItemByID(2)

#User Account to set
$UserAccount="Crescent\Salaudeen"
#To Add new List Item, use $Item = $List.AddItem()
$User = Get-SPUser -Identity $UserAccount -Web $web
$ListItem[$FieldName] = $User
$ListItem.Update()

Update Multiple Values People Picker Field using PowerShell script
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Variables
$SiteURL = "http://portal.crescent.com/sites/Sales"
$ListName = "Tasks"                
$FieldName="Assigned To"
 
#Get site and List objects
$web = Get-SPWeb $SiteURL
$list= $web.lists[$listName]

#Get the List Item to update
$ListItem  = $List.GetItemByID(2)

#User Account to set
$UserAccounts="Crescent\Salaudeen; Crescent\Ravi"
$UserAccountsColl = $UserAccounts -split ';'

$UserCollection = new-object Microsoft.SharePoint.SPFieldUserValueCollection
foreach($UserAccount in $UserAccountsColl)
{
    #Get the User
    $User=$web.EnsureUser($UserAccount)
    
    #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()
Get-Set Person or Group (People Picker) Field Value using PowerShell in SharePoint Get-Set Person or Group (People Picker) Field Value using PowerShell in SharePoint Reviewed by Salaudeen Rajack on 7:56 PM Rating: 5

2 comments:

  1. How to add multiple groups value in people or group field?

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.