SharePoint Online: PowerShell to a Get List

Requirement: Get a list in SharePoint Online using PowerShell

sharepoint online powershell get list

PowerShell to Get a SharePoint Online List

If you are looking for a way to get a list in SharePoint Online using PowerShell, this blog post will show you how to do just that. Let’s get a list in SharePoint Online and count items in a list using PowerShell script:

#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/sites/marketing"
$ListName="Project Tasks"
  
#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 list sharepoint online powershell
    $List=$Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()

    Write-host "Total Number of List Items:" $List.ItemCount
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

Replace the variables with the actual SharePoint Online site URL and name of your list, and make sure you have installed SharePoint Online CSOM SDK or SharePoint Online PowerShell Module.

SharePoint Online Get Lists using PowerShell

Similarly, you can get all lists from a SharePoint Online site as:

#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/sites/marketing"
  
#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 All Lists from the Web
    $Lists =$Ctx.Web.Lists
    $Ctx.Load($Lists)
    $Ctx.ExecuteQuery()

    #Iterate through each list
    ForEach($List in $Lists)
    {
        #get all list namses in sharepoint online site using powershell
        Write-host $List.Title
    }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

PnP PowerShell to Get a List in SharePoint Online

Here is how to get a list in SharePoint Online using the PnP PowerShell cmdlet Get-PnPList:

#Set Parameter
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ListName = "Migration Documents"

#Connect to the site
Connect-PnPOnline $SiteURL -Credentials (Get-Credential)

#powershell get sharepoint online list
$List = Get-PnPList -Identity $ListName

Write-host $List.ItemCount

To get all lists using PnP PowerShell, use:

#Set Parameter
$SiteURL="https://crescent.sharepoint.com/sites/marketing"

#Connect to the site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get all sharepoint online lists
$Lists = Get-PnPList

#Get the Title of All Lists
$Lists | Select Title

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.