SharePoint Online: How to Get a List using PowerShell?

Requirement: PowerShell to Get a List in SharePoint Online.

SharePoint Online: PowerShell to Get a List

PowerShell is a powerful scripting language that can be used to automate tasks and manage your SharePoint Online environment. This guide will show you how to use PowerShell to get a list in SharePoint Online.

Here is how to access a SharePoint Online list programmatically using PowerShell. Let’s get a list by title:

#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/sites/pmo" 
$ListName =  "Projects"
 
#Get Credentials to connect
$Cred = Get-Credential
 
Try {
    #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)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()

    #Get Total number of Items in the List
    Write-host "Total Items: " $List.ItemCount
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

You can use this script to get any list or library from a SharePoint Online site.

PnP PowerShell to Get a List in SharePoint Online

Let’s dive into how you can get a list in SharePoint Online using PnP PowerShell. The first thing you’ll need to do is connect to your SharePoint Online site using PowerShell and then use the PnP PowerShell cmdlet Get-PnPList to connect to the SharePoint Online list:

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName= "Projects"
  
#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -Interactive

Try {
    #sharepoint online get list using powershell
    $List = Get-PnPList $ListName -ThrowExceptionIfListNotFound -ErrorAction Stop
    
    #Get Total number of Items in the List
    Write-host "Total Items: " $List.ItemCount
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

This script retrieves the list with the title “Projects” in the SharePoint Online site. You can also retrieve lists by using the -Identity parameter of the Get-PnPList cmdlet, which accepts the list’s GUID. For example, to retrieve a list with the GUID “6f3c8f32-6a78-4c7a-a0a8-2f6f1f9a3b1c”, you would use the following command:

Get-PnPList -Identity 6f3c8f32-6a78-4c7a-a0a8-2f6f1f9a3b1c

To get all items in a SharePoint Online list, use: Get All Items from a SharePoint Online List using PowerShell

In summary, getting lists in SharePoint Online using PowerShell is a simple process that can be accomplished using the Get-PnPList cmdlet of PnP PowerShell or GetListByTitle method in CSOM. By following the examples provided in this article, you should be able to retrieve lists from various sources in SharePoint Online. To get all lists in SharePoint Online site using PowerShell, use: SharePoint Online PowerShell to Get All Lists

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 *