SharePoint Online: Get List by Title using PowerShell
Requirement: PowerShell to get a list by title in SharePoint Online.
SharePoint Online: PowerShell to Get List by Title
In SharePoint Online, PowerShell provides a powerful way to manage SharePoint lists and libraries. One common task when working with SharePoint lists in PowerShell is to retrieve a list by its title. In this blog post, we will review how to get a list by title in SharePoint Online using PowerShell.
Here is how to get a list by its 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" #Config Parameters $SiteURL="https://Crescent.sharepoint.com" $ListName="Documents" #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) $Ctx.load($List) $Ctx.ExecuteQuery() Write-host "Total Number of List Items:"$List.ItemCount
PnP PowerShell to Get a List by Its Title
If you’re looking for a quick way to get a list by its Title in SharePoint Online, Here is the PnP PowerShell to get a list or library from a SharePoint Online site:
#Set Parameters $SiteURL="https://Crescent.sharepoint.com/sites/marketing" $ListName= "Projects" #Connect to SharePoint Online Connect-PnPOnline -url $SiteURL -Interactive #Get the List by its Title $List = Get-PnPList -Identity $ListName $list | Select-Object Title, Id, BaseTemplate Write-host $List.ItemCount
Following the scripts provided in this article, you can easily retrieve a list by its title and perform operations on it using PowerShell.