SharePoint Online: PowerShell to Get All Document Libraries
Requirement: PowerShell to List All Document Libraries in SharePoint Online.
How to Get All Document Libraries in SharePoint Online Site?
If you are managing a SharePoint Online site and want to get all document libraries in a SharePoint Online site, there are two methods you can use! This blog post will show you how to get all document libraries in SharePoint Online.
The “Site Contents” page in SharePoint displays all lists and libraries of the site, along with the number of items in each list or library.
Let’s see the SharePoint Online PowerShell to list document libraries in a subsite (or site!).
SharePoint Online PowerShell to List Document Libraries
Let me share some handy scripts you need to quickly list all the document libraries for reporting or other purposes. Here is the PowerShell to get all document libraries from a 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"
#Config Parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
#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
#Get Lists from the web
$Ctx.Load($Ctx.Web.Lists)
$Ctx.executeQuery()
#Filter Document Libraries from Lists collection
$Lists = $Ctx.Web.Lists | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False}
#Loop through each document library and Get the Title
Foreach ($List in $Lists)
{
Write-host $List.Title
}
This gets all document libraries in SharePoint Online using PowerShell.
PnP PowerShell to Get All Document Libraries and their IDs
How do I get a list of document libraries in SharePoint Online using PowerShell? Well, here is the PnP PowerShell way using Get-PnPList cmdlet:
#Variables
$Site = "https://crescent.sharepoint.com/sites/retail"
#Connect with SharePoint Online
Connect-PnPOnline -Url $Site -Interactive
#Get all document libraries
$DocLibs = Get-PnPList | Where-Object {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false }
#Get ID and Title of the document library
$DocLibs | Select ID, Title
SharePoint Online PowerShell to List All Document Libraries in a Site Collection
Let’s wrap the script into a reusable function and add some error handling to get document libraries from a SharePoint Online site collection.
#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 documents Libraries in a SharePoint Online Site Collection
Function Get-SPODocumentLibrary($SiteURL)
{
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the web and Its subsites from given URL
$Web = $Ctx.web
$Ctx.Load($Web)
$Ctx.Load($Web.Lists)
$Ctx.Load($web.Webs)
$Ctx.executeQuery()
Write-host -f Yellow "Processing Site: $SiteURL"
#sharepoint online powershell list all document libraries
$Lists = $Web.Lists | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False}
#Loop through each document library and Get the Title
Foreach ($List in $Lists)
{
Write-host $List.Title
}
#Iterate through each subsite of the current web and call the function recursively
ForEach ($Subweb in $Web.Webs)
{
#Call the function recursively to process all subsites
Get-SPODocumentLibrary($Subweb.url)
}
}
Catch {
write-host -f Red "Error Getting Document Libraries!" $_.Exception.Message
}
}
#Config Parameters
$SiteCollURL="https://crescent.sharepoint.com"
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Call the function to get all document libraries in a site collection
Get-SPODocumentLibrary $SiteCollURL
To list all document libraries in SharePoint Online with PnP PowerShell, use: Get All Document Libraries in SharePoint Online using PnP PowerShell
Hi Salaudeen, thanks for sharing, one question, how could I add export-csv command in your script?, thanks in advance.
Sure! Just use: $Lists | Select -ExpandProperty Title | Out-File -FilePath “C:\Temp\Lists.csv”