SharePoint Online: PowerShell to Read List Items

Requirement: PowerShell to read list items in SharePoint Online

Read List Items in SharePoint Online using PowerShell

SharePoint Online: PowerShell to Read List Items

You can read SharePoint Online list with PowerShell in these three simple steps:

Step 1: Connect to SharePoint Online Site

To start with, we need the connection context to the SharePoint Online site.  

#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"
   
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/"
  
#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)

Step 2: Get List Items from SharePoint Online List

Once the connection is established, we can read or update any object from the SharePoint Online site. Here is how to get data from the SharePoint Online list using PowerShell.

#List Name Variable
$ListName = "Projects"
 
#Get the list
$List = $Ctx.Web.Lists.GetByTitle($ListName)
 
#Read All Items from the list
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
Write-host "Total Number of Items Found in the List:"$ListItems.Count

Step 3: Loop through List Items Collection to Read List Items

Once we get the list items collection, we can read list items by iterating through each list item. 

#Iterate through List Items
ForEach($Item in $ListItems)
{
    #sharepoint online powershell read list items 
    Write-Host ("List Item ID:{0} - Title:{1}" -f $Item["ID"], $Item["Title"])
}

PowerShell to Read SharePoint Online List

Here is the complete PowerShell script to read SharePoint Online List Items:

#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"
   
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/"
$ListName = "Projects"
  
#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 the list
$List = $Ctx.Web.Lists.GetByTitle($ListName)
 
#sharepoint online powershell read list items
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
Write-host "Total Number of Items Found in the List:"$ListItems.Count

#Iterate through List Items
ForEach($Item in $ListItems)
{
    #sharepoint online powershell read list items 
    Write-Host ("List Item ID:{0} - Title:{1}" -f $Item["ID"], $Item["Title"])
}

If you have a large list with >5000 items, You may have to batch process list items to avoid throttling issue: The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.” PowerShell to Get All List Items from Large Lists ( >5000 Items) in SharePoint Online

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

One thought on “SharePoint Online: PowerShell to Read List Items

  • February 10, 2021 at 8:33 PM

    Is there a way to get other field values like Name and Url? I can only ever get ID, Title, and Guid, which aren’t useful.

    Reply

Leave a Reply