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:

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
#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

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

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


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

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


Please Login and comment to get your questions answered!

Powered by Blogger.