Create List or Document Library using PowerShell in SharePoint Online

This PowerShell SharePoint tutorial explains, how to create a list or document library using PowerShell in SharePoint Online.

PowerShell Script to create list/document library in SharePoint online:

Below is the PowerShell script which can be used to create a list or document library in SharePoint Online.

function CreateSPOList {
    #All the pre required variable to connect SPO.
    $strSiteURL = "<< Site URL >>"
    $strUsrName = "<< Site User ID >>"
    $strLstTitle = "Loan Details"
    $strLstDesc = "Various loan details in bank!!!"
    $strLstTempID = 101
    #Reading Password from end user
    $strPWD = Read-Host "Please enter the password for $($strUsrName)" -AsSecureString
    #Creating object to SPO with the provided user name and password
    $ObjSPOCredls = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($strUsrName, $strPWD)
    # Creating client context object with the provided user name and password
    $ObjContext = New-Object Microsoft.SharePoint.Client.ClientContext($strSiteURL)
    $ObjContext.credentials = $ObjSPOCredls
    #create loan details list with the list template ID 101 (document library template)
    $ObjLstLoanDet = New-Object Microsoft.SharePoint.Client.ListCreationInformation
    $ObjLstLoanDet.title = $strLstTitle
    $ObjLstLoanDet.description = $strLstDesc
    $ObjLstLoanDet.TemplateType = $strLstTempID
    $ObjLst = $ObjContext.web.lists.add($ObjLstLoanDet)
    $ObjContext.load($ObjLst)
    # Sending request to SPO with all the pre loaded information.
    try{
        $ObjContext.executeQuery()
        write-host "Successfully Creates List $($strLstTitle)" -foregroundcolor green
    }
    catch{
        write-host "Error :: $($_.Exception.Message)" -foregroundcolor red
    }
}
CreateSPOList

To execute the PowerShell script follow the below steps:

Open SharePoint Online command let as administrator.

Execute the script to create the SharePoint list

Once you execute the script, you can see the output for PowerShell.

Create document library sharepoint online powershell

After this, the document library will be created and the output will be as follows:

Create document library sharepoint online powershell

You may like following PowerShell SharePoint tutorials:

Hope this PowerShell script helps to create a SharePoint list or document library using PowerShell in SharePoint Online.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 13+ years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019

>