Add Libraries to SharePoint Online using PowerShell

So for a while I was doing everything in the GUI for SharePoint, doing it this way can be quite slow and cumbersome depending on how SharePoint is feeling that day!!

If you want to skip this and create libraries through scripting.

First you want to download and install the SharePoint Online Management Shell

Now click on Start > Run > PowerShell (Run as Administrator) and run the following command:

Set-ExecutionPolicy RemoteSigned

This will allow the below script to be run,

Create a .ps1 file and paste the following in updating $siteURL, $adminURL and $userName for your SharePoint site.

This one will create a library called Test, you can just edit this $listTitle to make another.

function new-SPOnlineList {
    #variables that needs to be set before starting the script
    $siteURL = ""
    $adminUrl = ""
    $userName = ""
    $listTitle = "Test"
    $listDescription = "Test documents"
    $listTemplate = 101
    # Let the user fill in their password in the PowerShell window
    $password = Read-Host "Please enter the password for $($userName)" -AsSecureString
    # set SharePoint Online credentials
    $SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)
    # Creating client context object
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
    $context.credentials = $SPOCredentials
    #create list using ListCreationInformation object (lci)
    $lci = New-Object Microsoft.SharePoint.Client.ListCreationInformation
    $lci.title = $listTitle
    $lci.description = $listDescription
    $lci.TemplateType = $listTemplate
    $list = $context.web.lists.add($lci)
    #send the request containing all operations to the server
        write-host "info: Created $($listTitle)" -foregroundcolor green
        write-host "info: $($_.Exception.Message)" -foregroundcolor red

Now you say, I want to create lots of libraries at the same time! Well..

First download the SharePoint Online SDK and install

Now create a CSV file containing the following (please edit to your library names) called LibraryDetails.csv:

Library1, This is Library 1
Library2,This is Library 2
Library3, This is Library 3

This time around we are going to use another PowerShell script so create another .ps1 file with the contents:

Add-Type -AssemblyName Microsoft.VisualBasic
[System.Reflection.Assembly]::LoadFile("C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll") | Out-Null
[System.Reflection.Assembly]::LoadFile("C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll") | Out-Null
$file2 = "LibraryDetails.csv"
$csv = Import-Csv $file2 
$SPOAdmin = "" #Provide Admin account which can connect to your SharePoint Online Admin Center through powershell
$adminurl = "" #This will be your Tenant SharePoint Online Admin center url
$siteurl = "" # This is the site where your Document library will be created
$listTemplate = 101 #This where you will specify list template
$password = Read-Host "Please enter the password for $($userName)" -AsSecureString
$SPOCred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($SPOAdmin, $password)
foreach ($line in $csv)
$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$context.credentials = $SPOCred
$NewList = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$NewList.title = $line.ListTitle
$NewList.description = $line.Description
$NewList.TemplateType = $listTemplate
$list = $context.web.lists.add($NewList)
        write-host "info: Created $($line.listTitle)" -foregroundcolor green
        write-host "info: $($_.Exception.Message)" -foregroundcolor red

Now you can pre-type in all of your libraries and create them in one go! very handy!

Leave a Reply

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