GitHub Icon Image

Create list and libraries from CSV file

Summary

This script sample will bulk create lists or libraries using CSV file

Implementation

  • Open Windows PowerShell ISE
  • Edit Script and add required parameters for Site URL and path to CSV file
  • Press run
  • PnP PowerShell
  • CLI for Microsoft 365 with PowerShell
  • CSV file

###### Declare and Initialize Variables ######  

#Destination site collection url
$url="https://<tenant>.sharepoint.com/sites/yoursite"

#Path to CSV file
$csvFilePath = "ListsAndLibraries.csv"


# log file will be saved in same directory script was started from
$saveDir = (Resolve-path ".\")  
$currentTime= $(get-date).ToString("yyyyMMddHHmmss")  
$logFilePath=".\log-"+$currentTime+".log"  

## Start the Transcript  
Start-Transcript -Path $logFilePath 



## Connect to SharePoint Online site  
Connect-PnPOnline -Url $Url -Interactive

## Import CSV file
$data = Import-Csv -Path $csvFilePath -Delimiter ";"

## Create list or library
$data | Foreach-Object{
   
   New-PnPList -Title $_.Title -Url $_.Url -Template $_.Template -OnQuickLaunch -EnableContentTypes 
   
}  
 
## Disconnect the context  
Disconnect-PnPOnline  
 
## Stop Transcript  
Stop-Transcript  
  

Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell

Write-host 'ensure logged in'
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
  m365 login --authType browser
}


#Destination site collection url
$url="https://<tenant>.sharepoint.com/sites/<sitename>"
#Path to CSV file
$csvFilePath = "ListsAndLibraries.csv"


## Import CSV file
$data = Import-Csv -Path $csvFilePath -Delimiter ";"

## Create list or library
$data | Foreach-Object{
   m365 spo list add --title $_.Title --baseTemplate $_.Template --webUrl $url --output 'json'
   
} 

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

Title;Template;Url
PnP Library;DocumentLibrary;PnPLibrary
Announcements;Announcements;lists/Announcements
Custom Simple List;GenericList;lists/CustomSimpleList

Contributors

Author(s)
Valeras Narbutas

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Back to top Script Samples
Generated by DocFX with Material UI