Im trying to create a document library via a Power Shell script - failing at the first hurdle. To do this I need to declare the site url and admin url as below

#variables that needs to be set before starting the script
$siteURL = "https://domain.sharepoint.com/sites/SitA/SitePages/Home.aspx" (which is a subsite)
$adminUrl = "https://domain.sharepoint.com" is this classed as the admin url
$userName = "username@domain.com"

Have I got my understanding of the above right? Would the sub site which is in siteurl contain the templates such as document library and is the admin url the root level which contains multiple subsites?


To create document library

Use below PowerShell Command in SharePoint Online Management Shell:

function new-SPOnlineList {
    #variables that needs to be set before starting the script
    $siteURL = "https://spfire.sharepoint.com/sites/BlogDemo"
    $adminUrl = "https://spfire-admin.sharepoint.com"
    $userName = "mpadmin@spfire.onmicrosoft.com"
    $listTitle = "Finance"
    $listDescription = "Finance 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

Reference: http://www.sharepointfire.com/2016/01/create-new-document-library-sharepoint-online-powershell/

To Create Folder in Library

#Before Run the Script you must load the Client Assembly files   

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 createFolder{  

param ($SiteUrl, $ListURL, $FolderName, $UserName ,$Password)     

#Connect Office 365 SharePoint Online Site  
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))    

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)  
$Context.Credentials = $credentials  

#Get the List Root Folder  

#Create New Folder  
$Folder = $ParentFolder.Folders.Add($FolderName)  

Write-host "New Folder Created Successfully!"  

createFolder "https://<server>.sharepoint.com/" "Documents" "TutorialFolder" "<username>" "<password>"  
  • Thanks I got this working, next question :O) how do I create folders within my newly created document library using powershell? Thanks – Shabbaranks Mar 20 '17 at 13:20
  • @Shabbaranks I have updated my answer. Please upvote my answer if its helpful to you. :) – Dikesh Gandhi Mar 21 '17 at 3:40
  • Thanks just so I understand - the createFolder "https://<server>.sharepoint.com/" is this the site url as per the previous post? Thanks – Shabbaranks Mar 21 '17 at 11:12
  • Yes. Pass your site url. – Dikesh Gandhi Mar 21 '17 at 11:24

Use the below csom powershell to create a document library in SharePoint online

for that you need only 2 dlls for reference


copy the below powershell code in a file and save it as .ps1 and make sure the above dlls placed where this ps1 file exists for the reference. other wise you can hard code the file location in the given script

$host.Runspace.ThreadOptions = "ReuseThread"

function Create-NewListSPO 
    param ($sCSOMPath,$sSiteUrl,$sUserName,$sPassword,$sListName,$sListDescription) 

    #Adding the Client OM Assemblies         
    $sCSOMRuntimePath=$sCSOMPath +  "\Microsoft.SharePoint.Client.Runtime.dll"         
    $sCSOMPath=$sCSOMPath +  "\Microsoft.SharePoint.Client.dll"              
    Add-Type -Path $sCSOMPath          
    Add-Type -Path $sCSOMRuntimePath        

    #SPO Client Object Model Context 
    $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl) 
    $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword)   
    $spoCtx.Credentials = $spoCredentials       

    Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green 
    Write-Host "Creating Library $sListName in $sSiteUrl !!" -ForegroundColor Green 
    Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green         

    $spoListCreationInformation=New-Object Microsoft.SharePoint.Client.ListCreationInformation 

    Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green 
    Write-Host "Library $sListName created in $sSiteUrl !!" -ForegroundColor Green 
    Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green   
     Read-Host -Prompt "Press any key to close this"
catch [System.Exception] 
    Write-Host -ForegroundColor Red $_.Exception.ToString()    

$sSiteUrl = "https://domain.sharepoint.com/sites/dev"  # any site/sub site url 
$sUserName = "admin@domain.onmicrosoft.com"   # online user
$sListName= Read-Host -Prompt "Enter List Name "
$sListDescription="Using Powershell I am created" 
$sPassword = Read-Host -Prompt "Enter your password " -AsSecureString   
#$sPassword=ConvertTo-SecureString "<SPO_Password>" -AsPlainText -Force 
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath

Create-NewListSPO -sCSOMPath $dir -sSiteUrl $sSiteUrl -sUserName $sUserName -sPassword $sPassword -sListName $sListName -sListDescription $sListDescription

Use the below link to create different type of lists GenericList,DocumentLibrary,Surveys..etc



I know this is an older question but came up high in my search, however a simpler method you can do the following:

Install-Module SharePointPnPPowerShellOnline
Import-module SharePointPnPPowerShellOnline
Connect-PnPOnline -Url https://sitename.sharepoint.com/sites/Sitename -Credentials (Get-Credential) -ErrorAction Stop
New-PnPList -Title <enterlibraryname> -Template DocumentLibrary -OnQuickLaunch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.