SharePoint Online: PowerShell to Create a Document Library

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

Create Document Library using PowerShell in SharePoint Online:

The document library is the most commonly used type of library in SharePoint Online used to store and collaborate Office documents such as Word, Excel, PowerPoint, etc. SharePoint Online document libraries come with default metadata properties: “Type”, “Name”, “Modified”, and “Modified By” and can be extended based on our requirements. Make sure you have the “Edit” permission level to the site where you would like to create a document library before proceeding to the following steps! Let’s see how to create a new document library in SharePoint Online.

How to Create a document library in SharePoint Online?

Document libraries are a vital part of SharePoint Online, providing an easy way to collect and share files with colleagues. To create a new document library on the SharePoint Online site, follow these steps:

  1. Navigate to the SharePoint Online site in the web browser >> Click on Settings gear >> Choose “Site Contents”.
  2. On the Site Contents page, click on the “New” menu and choose the “Document library” option. (Or from the home page of your modern SharePoint site, click on the “New” button and then select “Document Library”)
    powershell script to create document library sharepoint online
  3. Provide a name and description for the library. Set the “Show in site navigation” Checkbox as per your requirement, and then click on the “Create” button to create the new document library.
    create document library in sharepoint online using powershell

This will create a new SharePoint document library, and your browser should take you there! Let’s see how to create libraries in SharePoint Online with PowerShell.

SharePoint Online: PowerShell to Create New Document Library using CSOM

Alright, How to create a document library in SharePoint Online using PowerShell? There are two ways to create a document library in SharePoint Online using PowerShell. The first is to use the SharePoint Online CSOM PowerShell, and the second is to use PnP PowerShell.

To create a document library, you’ll first need to connect to your SharePoint Online site using PowerShell. Once you’re connected, you can use the New-PnPList cmdlet in PnP PowerShell or Lists.Add() method in CSOM to create your library. You will need to specify a name and optional description for your library and choose an appropriate template. You can also specify additional options, such as the quick launch, template, and URL for the 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"

#Variables for Processing
$SiteURL = "https://Crescent.sharepoint.com/Sites/Sales"
$LoginName ="Salaudeen@Crescent.OnMicrosoft.com"
$LoginPassword ="Password" 

#Get the Client Context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)

#supply Login Credentials
$SecurePWD = ConvertTo-SecureString $LoginPassword -asplaintext -force  
$Credential = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($LoginName,$SecurePWD)
$Context.Credentials = $Credential

#powershell create document library sharepoint online
$ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListInfo.Title = "Project Docs"
$ListInfo.TemplateType = 101 #Document Library
$List = $Context.Web.Lists.Add($ListInfo)
$List.Description = "Repository to store project artifacts"
$List.Update()
$Context.ExecuteQuery()

write-host "New Document Library has been created!"

Make sure you change the $SiteURL and other variables matching your tenant. Let’s add some error handling to the above script and create a document library SharePoint Online with PowerShell.

How to Create a Document Library in SharePoint Online using PowerShell?

Document Library is a container to store any type of file on the SharePoint Online site. Here is the PowerShell for SharePoint Online to create a document library with some error handling added.

#Import SharePoint Online PowerShell Module
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
   
Function Create-DocumentLibrary()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $DocLibraryName
    )    
    Try {
    #Setup Credentials to connect
    $Cred = Get-Credential

    #Set up the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) 
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Get All Lists from the web
    $Lists = $Ctx.Web.Lists
    $Ctx.Load($Lists)
    $Ctx.ExecuteQuery()
 
    #Check if Library name doesn't exists already and create document library
    if(!($Lists.Title -contains $DocLibraryName))
    { 
        #create document library in sharepoint online powershell
        $ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
        $ListInfo.Title = $DocLibraryName
        $ListInfo.TemplateType = 101 #Document Library
        $List = $Ctx.Web.Lists.Add($ListInfo)
        $List.Update()
        $Ctx.ExecuteQuery()
  
        write-host  -f Green "New Document Library has been created!"
    }
    else
    {
        Write-Host -f Yellow "List or Library '$DocLibraryName' already exist!"
    }
}
Catch {
    write-host -f Red "Error Creating Document Library!" $_.Exception.Message
}
}
 
#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/Sales"
$DocLibraryName="Invoices"
 
#Call the function to create document library
Create-DocumentLibrary -siteURL $SiteURL -DocLibraryName $DocLibraryName
You can also specify the “URL” property to set the URL of a document library. E.g. You may want to remove the %20 characters from the URL!

Use this PowerShell to create a list in SharePoint Online: How to Create a List in SharePoint Online using PowerShell?

SharePoint Online: Add a Document Library using PnP PowerShell

Use the New-PnPList cmdlet to create a new document library. Here is how to create a document library in SharePoint Online using the PnP PowerShell script:

#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$LibraryName = "Project Documents"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#sharepoint online create document library powershell
New-PnPList -Title $LibraryName -Template DocumentLibrary -OnQuickLaunch

Once the document library is in place, you can start uploading files and folders to it: How do I upload an entire folder to SharePoint Online?

Alright, How about checking if a particular document library exists already, if not, create a new one? Well, We can wrap the above script into a re-usable function as:

#Function to Ensure a SharePoint Online document library
Function EnsurePnP-DocumentLibrary()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $LibraryName
    )    
    Try {
        Write-host -f Yellow "`nEnsuring Library '$LibraryName'"
         
        #Check if the Library exist already
        $List = Get-PnPList | Where {$_.Title -eq $LibraryName} 
        #Also works: $List = Get-PnPList -Identity $LibraryName

        If($List -eq $Null)
        {
            #Create Document Library
            $List = New-PnPList -Title $LibraryName -Template DocumentLibrary -OnQuickLaunch  
            write-host  -f Green "`tNew Document Library '$LibraryName' has been created!"
        }
        Else
        {
            Write-Host -f Magenta "`tA Document Library '$LibraryName' Already exist!"
        }
    }
    Catch {
        write-host -f Red "`tError Creating Document Library!" $_.Exception.Message
    }
}

#Connect to SharePoint Online
Connect-PnPOnline "https://crescent.sharepoint.com/Sites/Marketing" -Interactive

#Call the function to Ensure Document Library
EnsurePnP-DocumentLibrary -LibraryName "Branding"

Can a SharePoint site have more than one library? Of course! Multiple document libraries can be created for different purposes within one SharePoint site. If you want to create multiple document libraries in SharePoint Online from a CSV file, use: Create Multiple Document Libraries in SharePoint Online using PowerShell

How to Create Multiple Document Libraries in SharePoint Online?

You can have multiple document libraries in SharePoint. Just wrap the document library names in an Array and call the function to create a new library! E.g.,

$DocLibs = @(“Library1”, “Library2”, “Library3”)
ForEach($Library in $DocLibs)
{
#Call the function to create the library
EnsurePnP-DocumentLibrary -LibraryName $Library
}

Can I copy an Existing Document Library in SharePoint Online?

Yes! You can clone any existing document library by saving it as a list template workaround! How to Copy a Document Library in SharePoint Online using PowerShell?

How do I give permission to a document library in SharePoint Online?

Browse to the library settings >> On the List Settings page, click on the “Permissions for this Document Library” link >> break the permission inheritance. Now, click the “Grant Permissions” from the ribbon to provide access to users and groups. More Here: How to Grant Permission to a Document Library in SharePoint Online?

How to create a document library template in SharePoint Online?

To create a template from the SharePoint Online document library, navigate to your SharePoint Online Library >> Click on Settings >> Document Library Settings >> Click on “Save list as Template” under the Permissions and Management group.
More info: How to Create a Template from Document Library in SharePoint Online?

How do I upload an entire folder to SharePoint online?

To upload an entire folder to SharePoint Online, use the “Upload” button on the document library page and select “Folder” from the dropdown menu. Then, select the folder you want to upload and click “Open” to begin the upload process. Alternatively, you can use the OneDrive sync client to sync the folder to your computer and then drag and drop it into the SharePoint Online document library.

Can I add a document library on a SharePoint page?

Yes. You can add a document library on a SharePoint page. Simply go to the page where you want to add the library, click on the “Edit” button, and then select “Insert” from the top menu. From there, you can choose “Document Library” and select the library you want to add.
More info: How to add a document library to a Page in SharePoint Online?

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!

4 thoughts on “SharePoint Online: PowerShell to Create a Document Library

  • HI Salaudeen, can I use the New-PnPList -Title $LibraryName -Template DocumentLibrary -OnQuickLaunch also in creating a new document library based on a custom document library template saved on the site? I haven’t found an answer to this one yet, but maybe you have an idea how I can do this?

    Reply
  • Pls Provide script to create multiple document library in SPO using powershell

    Reply
    • You can create multiple document libraries from a CSV file! Use the link provided in the article.

      Reply

Leave a Reply

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