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. You can use PowerShell to retrieve a list by its name or its GUID. Here’s how to get a list in SharePoint Online using PowerShell:

Let’s get a list in SharePoint Online and count items in a list using the 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
}

This script retrieves all lists in a site, including hidden lists.

PnP PowerShell to Get a List in SharePoint Online

To get a list using PnP PowerShell, you first need to connect to your SharePoint site. You can do this by running the following command: Connect-PnPOnline -Url https://YourSite.sharepoint.com. Once you are connected, you can run the Get-PnPList command to get a list of all the lists on your site. If you want to get a specific list, you can use the Get-PnPList -Identity command. This will return the list with the specified name.

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

Get All Lists in a Site Collection using PnP PowerShell

How about iterating through all lists on a site?

#Parameters
$SiteURL="https://crescent.sharepoint.com/sites/Marketing"

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

#Get All Webs of the site collection
$WebsColl = Get-PnPSubWebs -IncludeRootWeb 

ForEach($Web in $WebsColl)
{
    Write-host "Processing Web:"$Web.Url -f Yellow
    
    #get All lists of the web - Exclude the hidden
    $AllLists = Get-PnPProperty -ClientObject $Web -Property Lists
    $Lists = $AllLists | Where {$_.Hidden -eq $false}

    #Loop through each list
    ForEach($List in $Lists)
    {
        Write-host "`t$($List.Title) has $($List.itemCount) items" -f Green
    }
}

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 *