Efficient Ways to Retrieve All Document Libraries in SharePoint using PowerShell

PowerShell is a powerful command-line automation and scripting tool that allows SharePoint administrators and developers to perform a variety of tasks and operations, such as managing sites, lists, users, workflows, and more. In SharePoint Online, there are two methods to retrieve all document libraries. This blog provides a step-by-step guide on how to use PowerShell to attain all document libraries.

 

Let’s explore how to use PowerShell in SharePoint Online to list document libraries in a subsite or site.

SharePoint Online PowerShell: List Document Libraries

Allow me to provide you with some useful sharepoint coding for efficiently listing all the document libraries in a SharePoint Online site, which can be helpful for reporting or other purposes:

#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 PowerShell script retrieves all the document libraries in SharePoint Online, enabling efficient management and manipulation of document libraries using PowerShell commands.

PnP PowerShell: Retrieve Document Libraries & IDs

Learn how to efficiently retrieve a list of document libraries in SharePoint Online using PowerShell with the help of the Get-PnPList cmdlet from the PnP PowerShell module:

#Variables

$Site = “https://crescent.sharepoint.com/sites/retail

#Connect with SharePoint Online

$Cred= Get-Credential

Connect-PnPOnline -Url $Site –Interactive $Cred;

#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: List Document Libraries in a Site Collection

Now let us understand how to create a reusable function with error handling in PowerShell to retrieve document libraries from a SharePoint Online site collection, allowing for efficient and reliable retrieval of data.

#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

You now have clear and concise knowledge about how the listing and retrieving of document libraries is processed.

In conclusion, utilizing PowerShell commands, such as Get-PnPList, can greatly simplify the process of retrieving all document libraries in a SharePoint Online site. By wrapping the script into a reusable function and incorporating error handling, you can streamline the task and ensure accurate results.