SharePoint Online: Get All Lists using PowerShell

Requirement: SharePoint Online Get All Lists inventory using PowerShell.

A SharePoint list is a container for information, similar to a table in a database. It enables you to store, organize, and share information with others in your organization. If you manage SharePoint Online, you may find yourself in a situation where you need to get a list or all lists on a site. You can do this using PowerShell. This article will show you how to use PowerShell to get all lists on a SharePoint Online site.

There are two ways to get all lists in SharePoint Online using PowerShell. The first way is to use the CSOM method and get Web.Lists collection. This will list of SPOList objects with various properties of the lists, such as title, URL, and item count. The second way is to use the Get-PnPList cmdlet with PnP PowerShell, which will retrieve all the lists in a SharePoint Online web.

Get All Lists in SharePoint Online using PowerShell CSOM:

In this blog post, we will take a look at how to use PowerShell to get a list of all the lists in a SharePoint Online site. This can be handy if you want to generate a list of all lists for auditing or reporting purposes, troubleshoot issues with lists, or simply get an overview of all the lists on the site. Let’s take a look at how it works!

Here is the PowerShell to get all lists in a SharePoint Online Site using CSOM:

#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"

#Function to Get all/specific list from site
Function Get-SPOList()
{
    Param
    (
        [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Web] $Web,
        [Parameter(Mandatory=$false)] [string] $ListName
    )
    #Get the Context
    $Ctx = $Web.Context
    
    #Get a single list or All Lists
    If($ListName)
    {
        #sharepoint online get list powershell
        $List = $Web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()
        Return $List
    }
    Else
    {
        #sharepoint online get all lists powershell
        $Lists = $Web.Lists
        $Ctx.Load($Lists)
        $Ctx.ExecuteQuery()
        Return $Lists
    }
}

#Parameters
$SiteURL="https://Crescent.sharepoint.com"

#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

#sharepoint online powershell get all lists
$Lists = Get-SPOList -Web $Ctx.Web

#Extract List data
$ListCollection = @()
ForEach($List in $Lists)
{
    $ListData = New-Object -TypeName PSObject
    $ListData | Add-Member -MemberType NoteProperty -Name "Title" -Value $List.Title
    $ListData | Add-Member -MemberType NoteProperty -Name "Itemcount" -Value $List.Itemcount
    $ListData | Add-Member -MemberType NoteProperty -Name "BaseTemplate" -Value $List.BaseTemplate
    $ListData | Add-Member -MemberType NoteProperty -Name "Created" -Value $List.Created
    $ListData | Add-Member -MemberType NoteProperty -Name "LastItemModifiedDate" -Value $List.LastItemModifiedDate
    $ListCollection += $ListData
}
#Export List Inventory to CSV
$ListCollection | Export-csv -Path "C:\Temp\list-inventory.csv" -NoTypeInformation

This PowerShell gets either a single list or all lists from the SharePoint Online site based on the given parameters.

How to Get a List in SharePoint Online using PowerShell?

To get a list in SharePoint Online, use:

#Call the function to get a List
$List = Get-SPOList -web $ctx.Web -ListName "Documents"
Write-host "Total Number of Items in List:" $List.ItemCount

PnP PowerShell to Get All Lists from SharePoint Online Site

You can use the Get-PnPList to get all lists in a site, including hidden lists. The following example gets all lists in the site https://crescent.sharepoint.com/sites/Marketing.

Let’s get all lists from the current SharePoint Online site.

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get all lists
$Lists = Get-PnPList

#Get List Title, Description and Number of Items
$Lists | Select Title, Description, ItemCount

You can also use the Identity parameter with the List Name, GUID, or List URL to get a specific list from the site. Similarly, to get a specific list in SharePoint Online using PnP PowerShell, use:

#Connect to PnP Online
Connect-PnPOnline -Url "https://Crescent.sharepoint.com" -Credentials (Get-Credential)
    
#Get the List
$List = Get-PnPList -Identity "Documents"

#Get Number of Items in the List
Write-host "Total Number of List Items:"$List.ItemCount

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!

2 thoughts on “SharePoint Online: Get All Lists using PowerShell

Leave a Reply

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