SharePoint Online: How to a Get List using PowerShell?

Requirement: SharePoint Online PowerShell to Get a List or document library.

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

Let’s see how to use the PnP PowerShell cmdlet Get-PnPList 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 - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

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