SharePoint Online: Create Multiple Document Libraries using PowerShell

Requirement: Create multiple document libraries in SharePoint Online using PowerShell.

How to Create Multiple Document Libraries in SharePoint Online?

Are you looking to create multiple document libraries in SharePoint Online? A single document library may not always be sufficient. Maybe you want to create a library for each department in your company or separate SharePoint libraries to store files for different projects you’re working on, or you may have a large number of documents that need to be organized. Whatever your reason, creating multiple document libraries is a best practice to manage your files and keep them safe and secure and get the most out of your SharePoint document management.

Sometimes, the default library “Document” may not be sufficient, and you may want to add additional document libraries to the site. Assuming you have edit permissions to the site (or member of Site owners or site members group), creating multiple document libraries in SharePoint Online is easy, and you can do it in just a few simple steps:

  1. Log into your SharePoint Online site >> Click on the ‘+ New’ button in the Menu bar >> Select ‘Document Library’ from the drop-down menu. how to create multiple document libraries in sharepoint online
  2. Enter a name and description for your document library, and then click Create. bulk create document library in sharepoint online

Repeat these steps for each additional library you wish to create. Once you have created your document library, you can upload new files (almost all file types) to it or create new folders to help organize your documents. You can also set permissions to your team members or employees and control who has access to your document library and further customize the library by adding fields, content types, setting permission inheritance, version history, Workflows, etc., from the library settings.

PowerShell to Create Multiple Document Libraries in SharePoint Online

Microsoft SharePoint is a versatile platform that can be used to manage and share data for collaboration in various ways. SharePoint team site comes with a default document library called “Documents”. Are you frequently creating document libraries on a regular basis? Using PowerShell, you can create multiple document libraries in no time, which can be a great way to provision document libraries in bulk at once.

How to create multiple document libraries in SharePoint Online? We can create multiple document libraries by wrapping the script into a reusable function and calling the function with an array of document libraries to create.

Is there a limit to the number of document libraries in SharePoint?
How many libraries can a SharePoint site have? Technically, you can have 2000 lists and libraries combined per site collection (including the root site and any subsites). Since the List View Threshold limit of 5000 impacts the performance and usability in SharePoint Online, It’s a good idea to create multiple document libraries.
#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 Create a SharePoint Online document library
Function CreateSPO-DocumentLibrary()
{

 [cmdletbinding()]

    param
    (
        [Parameter(Mandatory=$True,ValueFromPipeline)] [String[]] $LibraryNames,
        [Parameter(Mandatory=$False)] [String] $Description
    )    
    Try {
        #Create Each Document Library
        ForEach($LibraryName in $LibraryNames)
        {
            Write-host -f Yellow "`nEnsuring Document Library '$LibraryName'"
         
            #Get All Existing Lists from the web
            $Lists = $Web.Lists
            $Ctx.Load($Lists)
            $Ctx.ExecuteQuery()
 
            #Check if Library name doesn't exist already
            If(!($Lists.Title -contains $LibraryName))
            {
                #Create a new Document Library
                $ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
                $ListInfo.Title = $LibraryName
                $ListInfo.Description =  $Description
                $ListInfo.TemplateType =  [Microsoft.SharePoint.Client.ListTemplateType]::DocumentLibrary 
                $Web.Lists.Add($ListInfo) | Out-Null
                $Ctx.ExecuteQuery()
  
                write-host  -f Green "`tNew Document Library '$LibraryName' has been created!"
            }
            Else
            {
                Write-Host -f Magenta "`tA Document Library '$LibraryName' Already exists!"
            }
        }
    }
    Catch {
        write-host -f Red "`tError:" $_.Exception.Message
    }
}
 
#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
 
#Get Credentials to connect
$Cred = Get-Credential
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
   
#Get the web from URL
$Web = $Ctx.web
$Ctx.Load($Web)
$Ctx.executeQuery()
 
#Call the function to create document libraries
CreateSPO-DocumentLibrary -LibraryNames @("Document Library 1", "Document Library 2", "Document Library 3", "Document Library 4")    

Create Multiple Document Libraries in SharePoint Online using PowerShell

What if you want to add some additional parameters to the bulk document library script, such as “Quick Launch”? Here is my CSV file with document library details.

create multiple document libraries sharepoint online powershell

You can download the CSV template here:

Let’s create separate document libraries from a CSV file 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"
    
#Function to Create a SharePoint Online document library
Function CreateSPO-DocumentLibrary()
{
[cmdletbinding()]
    param
    (
        [Parameter(Mandatory=$True,ValueFromPipeline)] [String] $LibraryName,
        [Parameter(Mandatory=$False)] [String] $Description,
        [Parameter(Mandatory=$False)] [Switch] $ShowOnQuickLaunch
    )    
    Try {
            Write-host -f Yellow "`nEnsuring Document Library '$LibraryName'"
        
            #Get All Existing Lists from the web
            $Lists = $Web.Lists
            $Ctx.Load($Lists)
            $Ctx.ExecuteQuery()

            #Check if Library name doesn't exist already
            If(!($Lists.Title -contains $LibraryName))
            {
                #Set Quick Launch Option
                If($ShowOnQuickLaunch.IsPresent)
                {
                    $QuickLaunchOption = [Microsoft.SharePoint.Client.QuickLaunchOptions]::On
                }
                Else
                {
                    $QuickLaunchOption = [Microsoft.SharePoint.Client.QuickLaunchOptions]::Off
                }

                #Create Document Library
                $ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
                $ListInfo.Title = $LibraryName
                $ListInfo.Description =  $Description
                $ListInfo.TemplateType =  [Microsoft.SharePoint.Client.ListTemplateType]::DocumentLibrary 
                $List = $Web.Lists.Add($ListInfo)
                $List.OnQuickLaunch = $QuickLaunchOption
                $List.Update()
                $Ctx.ExecuteQuery()
 
                write-host  -f Green "`tNew Document Library '$LibraryName' has been created!"
            }
            Else
            {
                Write-Host -f Magenta "`tA Document Library '$LibraryName' Already exists!"
            }
    }
    Catch {
        write-host -f Red "`tError:" $_.Exception.Message
    }
}

#Set Parameters
$CSVFilePath = "C:\Temp\DocLibs.csv"

#Get Credentials to connect
$Cred = Get-Credential

#Get the CSV file
$CSVFile = Import-Csv $CSVFilePath 

#Read CSV file and create a document library
ForEach($Line in $CSVFile)
{
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Line.SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Get the web from URL
    $Web = $Ctx.web
    $Ctx.Load($Web)
    $Ctx.executeQuery()
    
    #Get the ShowOnQuickLaunch option
    $ShowOnQuickLaunch = [System.Convert]::ToBoolean($Line.ShowOnQuickLaunch)

    #Call the function to create document library
    If($ShowOnQuickLaunch -eq $True)
    {
        CreateSPO-DocumentLibrary -LibraryName $Line.LibraryName -Description $Line.Description -ShowOnQuickLaunch
    }
    Else
    {
        CreateSPO-DocumentLibrary -LibraryName $Line.LibraryName -Description $Line.Description
    }
}    

The “TemplateType” parameter defines the type of library. Use “[Microsoft.SharePoint.Client.ListTemplateType].GetEnumNames()” to get all available lists and libraries you can create in SharePoint Online. E.g., Picture Library, Announcements, etc.


Bulk Create Document Libraries with PnP PowerShell

This time, I must create multiple document libraries from a CSV file and set specific properties of the document library, such as Set version control (Major versions-minor versions), list experience, and permissions! The CSV file has two columns: LoginID and DisplayName.

create multiple sharepoint online document libraries from CSV file powershell

Use this PowerShell to bulk create document libraries from a CSV file input.

#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Shared"
$CSVFilePath = "C:\Users\salaudeen\Documents\Users.csv"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the CSV file
$CSVFile = Import-Csv $CSVFilePath
 
#Read CSV file and create new library
ForEach($Line in $CSVFile)
{
    Try {
        #Create document library
        Write-host -f Yellow "Creating Document Library:"$Line.DisplayName
        New-PnPList -Title $Line.DisplayName -Template DocumentLibrary -Url $Line.LoginID -ErrorAction Stop

        #Set Library Properties
        Set-PnPList -Identity $Line.DisplayName -EnableVersioning $True -EnableMinorVersions $True -CopyRoleAssignments:$False -BreakRoleInheritance -ListExperience NewExperience
        $Library  = Get-PnPList -Identity $Line.DisplayName

        #Exclude from Search Results
        $Library.NoCrawl = $True
        $Library.Update()

        #Grant permission on List to User
        $UserID = $Line.LoginID+"@crescent.com"
        Set-PnPListPermission -Identity $Line.DisplayName -AddRole "Full Control" -User $UserID

        Write-host -f Green "`tCreated Document Library:"$Line.DisplayName
    }
    Catch {
        write-host -f Red "`tError:" $_.Exception.Message
    }
}

You can use these scripts on OneDrive sites too.

Folders are still widely used over metadata. To create multiple folders in SharePoint Document Libraries, use: How to Bulk Add Folders to SharePoint Online?

How to get a document library in SharePoint Online using PowerShell?

Use: Get-PnPList -Identity “DocumentLibraryName” to get any document library from SharePoint Online site. You can also use the CSOM PowerShell script, $Ctx.Web.Lists.GetByTitle(“DocumentLibraryName”) to get a library.
More info: Get a Document Library in SharePoint Online using PowerShell

How do I get a list of files in the SharePoint Online document library using PowerShell?

You can use either the CSOM or PnP PowerShell scripts to get a list of all files in a SharePoint Online document library and export the inventory to a CSV file.
More info: How to get a list of files in SharePoint Online Document Library?

How to change SharePoint online document library URL?

To change the URL of a document library, You can use File Explore View, SharePoint Designer, or PowerShell methods.
More info: Rename a Document Library URL 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!

11 thoughts on “SharePoint Online: Create Multiple Document Libraries using PowerShell

  • It seems line 51 is overriding the URLS in my CSV file – I know nothing of programming/scripting so I am not sure how to make the script read the URLS from the file instead.

    Reply
    • I’ve updated the post with the CSV template. You can just change the SiteURL, LibraryName and other parameters in the CSV and use the PowerShell script to create document libraries from the CSV.

      Reply
  • Thank you Saludeen! Do you have another version of this to create document libraries for hundreds of different subsites? I used your script for creating the subsites and it saved me days! I created 1800 subsites in a matter of hours. But now I need to create doc libs for all of these sites.

    Reply
    • You can use the CSV file with different URLs and create a document library on the specific subsite.

      Reply
  • These SharePoint guides are truly wonderful, thanks for making them. Only one problem is that the script doesn’t take into account tenants with MFA and I imagine in 2021 most tenants will have MFA turned on (you truly need to do this if you haven’t already). I also know how to login to my tenant using Connect-PnPOnline -url XXXX – Interactive and also Connect-SPOService. However I cannot get the script (Create Multiple Document Libraries in SharePoint Online using PowerShell) to work because it seems to use credentials on line 69 and 79 that I haven’t figured out how to replace for MFA. I get this error:

    PS C:\Users\USRNAME\Desktop\PowerShell Scripts\sharepoint_scripts> .\CreateDocLibWQuickLaunch.ps1

    cmdlet Get-Credential at command pipeline position 1
    Supply values for the following parameters:
    Credential
    Exception calling “ExecuteQuery” with “0” argument(s): “The sign-in name or password does not match one in the
    Microsoft account system.”
    At C:\Users\USRNAME\Desktop\PowerShell Scripts\sharepoint_scripts\CreateDocLibWQuickLaunch.ps1:91 char:5
    + $Ctx.executeQuery()
    + ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : IdcrlException

    Ensuring Document Library ‘Doc Lib 9’
    Error: Exception calling “ExecuteQuery” with “0” argument(s): “The sign-in name or password does not match one in the Microsoft account system.”
    Exception calling “ExecuteQuery” with “0” argument(s): “The sign-in name or password does not match one in the
    Microsoft account system.”
    At C:\Users\USRNAME\Desktop\PowerShell Scripts\sharepoint_scripts\CreateDocLibWQuickLaunch.ps1:91 char:5
    + $Ctx.executeQuery()
    + ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : IdcrlException

    Ensuring Document Library ‘Doc Lib 10’
    Error: Exception calling “ExecuteQuery” with “0” argument(s): “The sign-in name or password does not match one in the Microsoft account system.”

    It keep complaining about line 91 char 5:
    $Ctx.executeQuery()

    Please help…

    Reply
  • I have configured MFA in my tenant, and need help in authenticating using MFA.

    Reply
  • Hi,

    Do you have a solution to creating multiple document libraries from a CSV on Sharepoint On Prem 2016?

    Reply
  • I need to create multiple Custom lists, document libraries, Picture libraries, Tasks, etc. Please help.

    Reply

Leave a Reply

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