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 Iterate Through List Items in SharePoint Online

PowerShell to Loop through List Items in SharePoint Online

As an administrator of SharePoint Online, there may be times when you need to loop through a list of items in SharePoint Online. PowerShell provides a handy way to do this. This can be useful if you need to perform tasks such as exporting data from a list or performing bulk operations on each item in the list. This post will explore how to use PowerShell to iterate through list items.

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://Crescent.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, loops through each item, and gets the list item’s title.

Iterate through List Items in SharePoint Online using PnP PowerShell

Looping through list items in SharePoint Online using PowerShell can be useful when you need to perform a specific action on each item in a list. Looping through SharePoint Online list items using PnP PowerShell scripts is much simpler:

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName ="Projects"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get all list items
$ListItems = Get-PnPListItem -List $ListName -PageSize 2000

ForEach($ListItem in $ListItems)
{
    #Get List Item ID
    Write-host $ListItem.ID
}

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 - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

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

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

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

      Reply

Leave a Reply

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