SharePoint Online: PowerShell to Loop Through List Items

Requirement: SharePoint Online PowerShell to Loop through List Items

Make sure you have SharePoint Online Client SDK downloaded and installed in your machine before running the script: https://www.microsoft.com/en-us/download/details.aspx?id=42038

PowerShell to Loop through List Items in SharePoint Online

Here is a basic example for iterating through each list item in a SharePoint Online list or library.

#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"
   
#Config Parameters
$SiteURL="https://crescenttech.sharepoint.com"
$ListName="Projects"
  
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
   
#Get the List
$List=$Ctx.Web.Lists.GetByTitle($ListName)

#Get All List items
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()

Write-host "Total Number of List Items found:"$ListItems.count

#sharepoint online powershell loop through list items
Foreach ($Item in $ListItems)
{
    #Get the List Item's Title
    Write-host $Item["Title"]
}

This PowerShell script gets all items from the given list, loop through each item and gets the list item’s title.

Similarly, you can loop through each document in a document library using PowerShell: SharePoint Online: PowerShell to Iterate Through All Files in a Document Library

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!

2 thoughts on “SharePoint Online: PowerShell to Loop Through List Items

  • November 5, 2020 at 10:50 AM

    I have a column name Site URL in my list, how can i get that value inside a for loop

    Reply
    • November 14, 2020 at 7:00 PM

      Just use: $Item[“SiteURL”] , Here the SiteURL is the internal name of the column value you want to retrieve.

      Reply

Leave a Reply