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:

  • Navigate to the SharePoint Online site in the web browser >> Click on Settings gear >> Choose “Site Contents”.
  • 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
  • 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?

#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 ="[email protected]"
$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
    $Cred = 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"
$DocLibraryName="Project Docs"

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

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

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

  • 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.