SharePoint Online: Get a Document Library using PowerShell

Requirement: Get a Document Library in SharePoint Online using PowerShell.

sharepoint online powershell get document library

SharePoint Online: Get a Document Library using PowerShell

When working with PowerShell and SharePoint Online, there may be times you need to get a document library. There are many ways to interact with SharePoint Online from Powershell. This blog post will look at how to get a document library using PowerShell. This can be useful for automating tasks or reporting purposes. Let’s get started!

Here is the PowerShell to Get a SharePoint Online document library:

#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"

$SiteURL = ""

#Setup Credentials to connect
$Cred = Get-Credential

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
    #sharepoint online get a document library powershell
    $DocLibrary = $Ctx.Web.Lists.GetByTitle($DocLibraryName)

    Write-host "Total Number of Items in the Document Library:"$DocLibrary.ItemCount
Catch {
    write-host -f Red "Error:" $_.Exception.Message

PnP PowerShell to get a document library in SharePoint Online:

To get a document library using PowerShell, you will first need to connect to your SharePoint Online site using the Connect-PnpOnline cmdlet. Once you have connected, you can use the Get-PnPList cmdlet to get your document library. You will need to specify a few parameters, such as the name of the library and the site URL, and PowerShell will take care of the rest.

To get a document library in the SharePoint Online site, Here is the PnP PowerShell script:

$SiteURL = ""
$LibraryName = "Documents"

#Connect to the Site
Connect-PnPOnline -URL $SiteURL -Interactive

#sharepoint online powershell get document library
$DocumentLibrary = Get-PnPList -Identity $LibraryName

#Get Number of Items the Document Library
Write-host $DocumentLibrary.ItemCount

This script gets the document library and returns the total number of items from the library.


In summary, getting a document library in a SharePoint Online site using PowerShell is a straightforward process. By connecting to the SharePoint Online site, you can obtain a specific document library or get a list of all document libraries on a site. This can be useful for managing or processing large amounts of content. The example script provided in this tutorial can be easily modified to suit your specific needs.

How do I get a list of document libraries in SharePoint Online using PowerShell? Use: SharePoint Online: PowerShell to Get 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!

One thought on “SharePoint Online: Get a Document Library using PowerShell

  • Thank you for the tutorial. I am trying to get acces in a subfolder of the Documents library with a PowerShell script.
    Entering the full folder path (ex: Documents/Test), gives me an exception : “Cannot contact site at the specified URL…” – ClientRequestException.

    Do you know how to send a file to a subfolder with PowerShell?


Leave a Reply

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