SharePoint Online: PowerShell to Get List Item Field Value
Requirement: PowerShell to Get List Item Field Value in SharePoint Online.
SharePoint Online PowerShell to Get List Item Field Value
As a SharePoint Online administrator, there will be times when you need to get the field value of a list item through PowerShell. In this blog post, I’ll show you how to use PowerShell to get the field value of a list item.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Parameters
$SiteURL = "https://crescent.sharepoint.com"
$ListName = "Projects"
$FieldName = "Manager"
$ItemId="1"
#Set Authentication Parameters
$UserName = "[email protected]"
$Password = "Password1"
$SecurePassword= $Password | ConvertTo-SecureString -AsPlainText -Force
#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
#Get List Item
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ItemId)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()
#Get the Field Value
$ListItem[$FieldName]
#Also does the same: Get the Field Value
#$ListItem.FieldValues[$FieldName]
The above script returns data as per the field type. E.g., as the “Manager” field is “Person or Lookup”, this PowerShell script returns Microsoft.SharePoint.Client.FieldUserValue data type. Let’s get only the text value from a field as:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Parameters
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Projects"
$FieldName = "Manager"
$ItemId="1"
#Get credentials to connect
$Cred = Get-Credential
#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName, $Cred.Password)
#Get List Item
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ItemId)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()
#Get List Item Field Values as Text
$ListItemFieldValues = $ListItem.FieldValuesAsText
$Ctx.Load($ListItemFieldValues)
$Ctx.ExecuteQuery()
#Get the Text value of "Manager" field from the list Item
$ListItemFieldValues[$FieldName]
PnP PowerShell to Get a List Item’s Field Value
With PnP PowerShell, we can get the value of a field from a list item as:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Pmo"
$ListName = "Projects"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get a Most recent List Item created
$ListItem = Get-PnPListItem -List $ListName -PageSize 500 | Select -Last 1
#Get All Field Values from the List Item
$ListItem.FieldValues
Here are my other PowerShell scripts to get data from list items in SharePoint Online: