how to get all lists in a ShrePoint site using SharePoint Online Management Shell cmdlets

35 Views Asked by At

when I was working with SharePoint Online using PowerShell, I had a requirement to get all the List collection present in my SharePoint site.

I got all the lists using PnP PowerShell commands. But i didn't get the list collection using the SharePoint Online Management shell. Suggest me the SharePoint Online Management Shell command to get all the lists.

2

There are 2 best solutions below

0
Ganesh Sanap - MVP On

SharePoint Online PowerShell (SharePoint Online Management Shell) does not have any straight forward command like PnP PowerShell to get all lists from the SharePoint site.

You will have to use the CSOM methods for this requirements, like:

#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://<tenant>.sharepoint.com/sites/MySite"
 
#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

Replace $SiteURL variable value with your SharePoint site URL.

0
Xyza_MSFT On

If you want to using PnP PowerShell commands, you need to Install the PnP PowerShell Module for SharePoint Online.

Install-Module PnP.PowerShell

Try to use the following PnP powershell to 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