SharePoint Online: How to a Get List using PowerShell?

Requirement: SharePoint Online PowerShell to Get List.

SharePoint Online: Get List from PowerShell

If you are managing SharePoint Online, managing lists is a common requirement as part of your job. In this blog post, we will be looking at how to use PowerShell to get a list from the SharePoint Online site. We will also show you how to get a property from a list.

Here is the PowerShell to Get SharePoint Online List:

#sharepoint online get list powershell
Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking

#Config Variables for Site URL, List Name
$SiteURL= "https://crescent.sharepoint.com/sites/sales"
$ListName="Sales Contacts"
 
#Setup 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)
 
    #powershell sharepoint online get list
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()

    Write-host "Total Number of Items in the List:"$List.ItemCount
}
 Catch [Exception] {
      Write-host $_.Exception.Message -f Red
} 

This PowerShell gets a list by its title or name from SharePoint Online.

PnP PowerShell to Get a List in SharePoint Online

Here is how to use PnP PowerShell to get a SharePoint Online list:

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

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

    #Get a list from Sharepoint Online using PnP PowerShell
    $List = Get-PnPList -Identity $ListName

    #Get Number of Items in the List
    Write-host $List.ItemCount
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

This script gets the given list and the number of items from the list. To get all lists in SharePoint Online site using PowerShell, use: SharePoint Online PowerShell to Get All Lists

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!

Leave a Reply