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.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>