Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

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

$
0
0
Here is my collection of PowerShell scripts to work with People picker field values:

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.abraaj.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.abraaj.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.abraaj.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.abraaj.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()

Viewing all articles
Browse latest Browse all 1058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>