SharePoint Online: Get All Lists and Libraries using PowerShell

Are you looking for a way to get an inventory of all lists and libraries in your SharePoint Online site? This can be helpful for auditing or simply getting an overview of what is on your site. In this article, we’ll show you how to use PowerShell to get detailed information about all lists and libraries.

SharePoint Online: PowerShell to Get All Lists

While working with SharePoint Online, you may have to iterate through all lists on a site. Here is the PowerShell script to get all lists and libraries from the given SharePoint Online site:

#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"
  
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/sales"
$UserName="salaudeen@crescent.com"
$Password ="Password goes here"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))

Try { 
    #Set up the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
    $Context.Credentials = $credentials

    #sharepoint online powershell get all lists
    $Lists = $Context.web.Lists
    $Context.Load($Lists)
    $Context.ExecuteQuery()

    #Iterate through each list in a site   
    ForEach($List in $Lists)
    {
        #Get the List Name
        Write-host $List.Title
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

This PowerShell gets all list names from the given SharePoint Online site.

Get the Inventory of All Lists in a SharePoint Online Site and Export to CSV

Let’s dig a bit deeper and get all list properties and export them to a CSV file:

#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 Variable
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$CSVPath = "C:\Temp\ListInventory.csv"

#Setup Credentials to connect
$Cred = Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
   
    #Get Lists from the site
    $Web = $Ctx.Web
    $Lists = $Web.Lists
    $Ctx.Load($Web)
    $Ctx.Load($Lists)
    $Ctx.ExecuteQuery()

    $ListDataCollection = @()
    #Get List details
    ForEach ($List in $Lists)
    {
        $ListData = New-Object PSObject -Property ([Ordered] @{
        ListName = $List.Title
        Description = $List.Description
        ItemCount = $List.ItemCount
        BaseTemplateID = $List.BaseTemplate
        Created = $List.Created 
        BaseType = $List.BaseType
        ContentTypesEnabled = $List.ContentTypesEnabled
        Hidden = $List.Hidden
        ListId = $List.Id
        IsCatalog = $List.IsCatalog 
        LastItemDeletedDate = $List.LastItemDeletedDate 
        LastItemModifiedDate = $List.LastItemModifiedDate 
        ParentWebUrl = $List.ParentWebUrl 
        VersioningEnabled = $List.EnableVersioning    
        })
        $ListDataCollection += $ListData
    }
    $ListDataCollection

    #Export List data to CSV
    $ListDataCollection | Export-Csv -Path $CSVPath -Force -NoTypeInformation
    Write-host -f Green "List Statistics Exported to CSV!"
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

SharePoint Online: PowerShell Script to Iterate through all lists and libraries in a site collection

Now, let’s make this code into a re-usable function and get lists and libraries from all sites and subsites from the given site.

#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"
  
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/sales"
        
#Get Credentials to connect
$Cred= Get-Credential
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

Try {
    #Function to Get all lists from the web
    Function Get-SPOList($Web)
    {
        #Get All Lists from the web
        $Lists = $Web.Lists
        $Context.Load($Lists)
        $Context.ExecuteQuery()

        #Get all lists from the web   
        ForEach($List in $Lists)
        {
            #Get the List Name
            Write-host $List.Title
        }
    }

    #Function to get all webs from given URL
    Function Get-SPOWeb($WebURL) 
    {
        #Set up the context
        $Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
        $Context.Credentials = $Credentials

        $Web = $context.Web
        $Context.Load($web)
        #Get all immediate subsites of the site
        $Context.Load($web.Webs)  
        $Context.executeQuery()
 
        #Call the function to Get Lists of the web
        Write-host "Processing Web :"$Web.URL
        Get-SPOList $Web
 
        #Iterate through each subsite in the current web
        foreach ($Subweb in $web.Webs)
        {
            #Call the function recursively to process all subsites underneaththe current web
            Get-SPOWeb($SubWeb.URL)
        }
    }

    #Call the function to get all sites
    Get-SPOWeb $SiteUrl
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Script Output:

powershell to get all lists in sharepoint online

PnP PowerShell to Get All Lists and Libraries in a Site Collection

Let’s get all Lists and Libraries Inventory of All Sites in a Site Collection and Export it to CSV file using PnP PowerShell.

#Function to Get Lists and Libraries of a web
Function Get-SPOSiteInventory([Microsoft.SharePoint.Client.Web]$Web)
{
    Write-host -f Yellow "Getting Lists and Libraries from site:" $Web.URL

    #Get all lists and libraries
    $SiteInventory= @()
    $Lists= Get-PnPProperty -ClientObject $Web -Property Lists
    foreach ($List in $Lists)
    {
        $Data = new-object PSObject
        $Data | Add-member NoteProperty -Name "Site Name" -Value $Web.Title
        $Data | Add-member NoteProperty -Name "Site URL" -Value $Web.Url
        $Data | Add-member NoteProperty -Name "List Title" -Value $List.Title
        $Data | Add-member NoteProperty -Name "List URL" -Value $List.RootFolder.ServerRelativeUrl
        $Data | Add-member NoteProperty -Name "List Item Count" -Value $List.ItemCount
        $Data | Add-member NoteProperty -Name "Last Modified" -Value $List.LastItemModifiedDate
        $SiteInventory += $Data
    }

    #Get All Subwebs
    $SubWebs = Get-PnPProperty -ClientObject $Web -Property Webs
    Foreach ($Web in $SubWebs)
    {
        $SiteInventory+= Get-SPOSiteInventory -Web $Web
    }
    Return $SiteInventory
}

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$CSVFile = "C:\temp\SiteData.csv"

Try { 
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive

    #Get the Root Web
    $Web = Get-PnPWeb

    #Call the function and export results to CSV file
    Get-SPOSiteInventory -Web $Web | Export-CSV $CSVFile -NoTypeInformation
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

To get all document libraries in SharePoint Online using PowerShell, use: SharePoint Online: PowerShell to List All Document Libraries

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

12 thoughts on “SharePoint Online: Get All Lists and Libraries using PowerShell

  • Hello all,
    Did someone managed to resolve the error:
    Error Generating Site Permission Report! Cannot process argument transformation on parameter ‘Web’. Cannot convert the “Microsoft.SharePoint.Client.Web” value of type “Microsoft.SharePoint.Client.Web” to type “Microsoft.SharePoint.Client.Web”. I have run Update-Module SharePointPnPPowerShellOnline but the error persist.
    Thank you in advance for the answer!

    Reply
  • “PnP PowerShell to Get All Lists and Libraries in a Site Collection”

    This script is excellent. However, the “List URL” column in the CSV is blank.
    What could be the cause?

    The version of PnP.PowerShell is 1.10.0.

    Reply
  • For those getting the error about Cannot convert the “microsoft.sharepoint.client.web”, the fix for me was to remove the [Microsoft.SharePoint.Client.Web] from the first line in the function. It looks like this
    Function Get-SPOSiteInventory($Web)

    If you have MFA, the Connect-PnPOnline worked for me with just
    Connect-PnPOnline -Url $SiteURL

    Reply
  • Hello. I’m getting the same ‘Error Generating Site Permission Report! Cannot process argument transformation on parameter ‘Web’. Cannot convert the “Microsoft.SharePoint.Client.Web” value of type “Microsoft.SharePoint.Client.Web” to type “Microsoft.SharePoint.Client.Web”‘. Update-Module SharePointPnPPowerShellOnline didn’t work. Did you solve this?? Thank u very much.

    Reply
  • Error: WARNING: A newer version of PnP PowerShell is available: 3.10.1906.0. Consider upgrading.
    Error: Cannot process argument transformation on parameter ‘Web’. Cannot convert the “Microsoft.SharePoint.Client.Web” value of type “Microsoft.SharePoint.Client.Web” to
    type “Microsoft.SharePoint.Client.Web”.

    Reply
    • Use: Update-Module SharePointPnPPowerShellOnline to update the PnP Module for SharePoint Online!

      Reply
    • I’m having the same issue and I’m using the latest version of PnP Module.

      Did you manage to find a solution?

      Reply
  • HI,
    I am getting authentication Error message ..I am ensured that i have entered correct user name and password and MFA is disabled. Please let me know how to resolve the issue .
    Connect-SPOService : The sign-in name or password does not match one in the Microsoft account system.
    At D:kotiScriptssample3.ps1:8 char:1
    + Connect-SPOService -Url $turl -Credential $credential
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Connect-SPOService], IdcrlException
    + FullyQualifiedErrorId : Microsoft.SharePoint.Client.IdcrlException,Microsoft.Online.SharePoint.PowerShell.ConnectSPOService

    Reply
    • You can’t authenticate by hard-coding credentials if MFA is enabled for the Account. So, just remove the “-credentials” parameter and run the script, which prompts for login prompt that’s MFA aware!

      E.g: Connect-SPOService -Url $SiteURL

      Reply
      • I remove the credentials parameter and then receive the following error: Error: Exception calling “ExecuteQuery” with “0” argument(s): “The remote server returned an error: (403) Forbidden.”

        Is there an easier way to run this script with a user that has MFA enabled?

        Reply
  • Hi Salaudeen,

    For SharePoint on-premises, Can you publish another Powershell script to Iterate through all lists and libraries in Web Application and site collection level (Output like CSV file).

    Thanks,
    SV
    PowerShell Script

    Reply

Leave a Reply

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