How to Get All Lists and Libraries from SharePoint Online Site using PnP PowerShell?

Recently, I was working on a SharePoint Online migration project, where we were required to get all lists and libraries from SharePoint Online sites. I used PnP PowerShell for this. In this tutorial, I will show you how to get all lists and libraries from a SharePoint Online site using PnP PowerShell.

Get All Lists and Libraries from a SharePoint Online Site using PnP PowerShell

To get all the lists and libraries of a SharePoint Online site using PnP PowerShell, you can use the Get-PnPList cmdlet. This command will return all the lists and libraries from the SharePoint site.

Below is the complete script. It will return the Title, URL, and Creation Date.

$siteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs"
Connect-PnPOnline -Url $siteUrl -Interactive
$lists = Get-PnPList
foreach ($list in $lists) {
    Write-Host "Name: $($list.Title)"
    Write-Host "URL: $($list.RootFolder.ServerRelativeUrl)"
    Write-Host "Created: $($list.Created)"
    Write-Host "-----------------------------"
}

I executed the above script, and you can see it returns all the lists and libraries from the SharePoint Online site.

get all lists and libraries sharepoint pnp powershell

The above examples did not show us if it is a list or a library. If you want to type, whether it is a list or a library, you can modify the PowerShell script like the one below.

Note: The template ID for the list is 100, and for the library, it is 101.

$siteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs"
Connect-PnPOnline -Url $siteUrl -Interactive
# Retrieve all lists and libraries
$lists = Get-PnPList

# Function to determine type
function Get-ListType {
    param (
        [int]$baseTemplate
    )
    switch ($baseTemplate) {
        101 { return "Library" }
        100 { return "List" }
        default { return "Other" }
    }
}

# Display list details with type
foreach ($list in $lists) {
    $listType = Get-ListType -baseTemplate $list.BaseTemplate
    Write-Host "Name: $($list.Title)"
    Write-Host "URL: $($list.RootFolder.ServerRelativeUrl)"
    Write-Host "Type: $listType"
    Write-Host "Created: $($list.Created)"
    Write-Host "-----------------------------"
}

You can also see the output in the screenshot below; it displays the type.

get all lists and libraries sharepoint powershell

Check out Copy Items from One List to Another in SharePoint Online Using PnP PowerShell

Get All Lists in SharePoint Site using PnP PowerShell

Sometimes, you might required to get all the lists from the SharePoint site without the libraries. So, here is the PowerShell script to get all lists from a SharePoint site using PnP PowerShell.

$siteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs"
Connect-PnPOnline -Url $siteUrl -Interactive
# Retrieve all lists and libraries
$lists = Get-PnPList

$allLists = @()

foreach ($list in $lists) {
    if ($list.BaseTemplate -eq 100) {
        $allLists += $list
    } 
}

# Display all lists
Write-Host "Lists:"
foreach ($list in $allLists) {
    Write-Host "Name: $($list.Title)"
    Write-Host "URL: $($list.RootFolder.ServerRelativeUrl)"
    Write-Host "Created: $($list.Created)"
    Write-Host "-----------------------------"
}

I executed the above PnP PowerShell script, and you can see it displayed all the lists from my SharePoint site. You will also get the exact output.

get all lists in SharePoint Online using PnP PowerShell

Check out Recover a SharePoint Online Site Using PowerShell and PnP PowerShell

Get All Libraries from a SharePoint Online Site using PnP PowerShell

In this same way, let me show you how to get all the libraries from a SharePoint Online site using PnP PowerShell. This is also a very common requirement among PowerShell SharePoint developers.

Here is the complete script to get all the SharePoint document libraries using PnP PowerShell.

$siteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs"
Connect-PnPOnline -Url $siteUrl -Interactive
# Retrieve all lists and libraries
$lists = Get-PnPList

$allLibraries = @()

foreach ($list in $lists) {
    if ($list.BaseTemplate -eq 101) {
        $allLibraries += $list
    } 
}

# Display all libraries
Write-Host "Libraries:"
foreach ($library in $allLibraries) {
    Write-Host "Name: $($library.Title)"
    Write-Host "URL: $($library.RootFolder.ServerRelativeUrl)"
    Write-Host "Created: $($library.Created)"
    Write-Host "-----------------------------"
}

I executed the above PowerShell script, and you can see the output in the screenshot below.

get all libraries in SharePoint Online using PnP PowerShell

Conclusion

In this tutorial, I have explained how to get all lists and libraries in SharePoint Online using PnP PowerShell. Also, I created two different scripts:

  • Get all lists from a SharePoint Online site using PnP PowerShell
  • Get all libraries from a SharePoint Online site using PnP PowerShell.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.