Add multiple lists or libraries using csv file
This script sample shows how to bulk create lists or libraries using 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
###### 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
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'
} 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.
Tool documentation & references
- Want to learn more about CLI for Microsoft 365 and the commands
Check out the CLI for Microsoft 365 site to get started and for the reference to the commands.
- Want to learn more about PnP PowerShell and the cmdlets
Check out the PnP PowerShell site to get started and for the reference to the cmdlets.