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.

sharepoint online powershell get list item field value
#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 = "Salaudeen@Crescent.com"
$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()    

#sharepoint online powershell to get list item 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

Retrieving the field values for a list item in SharePoint Online can be a useful way to extract data and automate tasks. With PnP PowerShell, you can easily get a list of field values for a list item in SharePoint Online using the Get-PnPListItem cmdlet.

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

To get all field values, use the following:

#Get All Field Values from the List Item
$FieldValues = $ListItem.FieldValues

ForEach ($Field in $FieldValues.Keys)
{
    Write-Host "$Field : $($FieldValues[$Field])"
}

With this script, you can get a list of field values for a SharePoint Online list item using PowerShell. This can be a useful way to extract data from your SharePoint lists and automate tasks. Here are my other PowerShell scripts to get data from list items in SharePoint Online:

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *