While taking SharePoint lists/libraries as a data source for Power Apps applications with PnP PowerShell cmdlets, we create or delete the SharePoint lists/libraries quickly. It avoids mistakes and saves time.
In this article, I will explain how to create and delete SharePoint document library using PowerShell.
NOTE:
Make sure you have registered an Application in Microsoft Entra Admin Center and have AllSites.Read permission. Keep the Application(client ID) to run the PnP Power Shell script.
Create and Delete SharePoint Document Library Using PowerShell [Single Library]
Here, we’ll see how to create a single SharePoint document library Using PnP Power Shell. We need to use New-PnPList cmd to create a SharePoint document library; the syntax is :
New-PnPList -Title "ITDocuments" -Template DocumentLibrary
Here,
- Title = Provide SharePoint document library name.
- Template = It defines whether we’re creating a SharePoint list or a document library. Providing GenericList will be considered a list, and DocumentLibrary will be a library.
To rename the SharePoint document library name, we need to use Set-PnPList cmd; the syntax is :
Set-PnPList -Identity "ITDocuments" -Title "IT Documents"
Here,
- Identity = library’s internal name/ previous name.
- Title = Provide the new name for the document library.
To delete the SharePoint document library, we need to use Remove-PnPList cmd. The syntax is:
Remove-PnPList -Identity "IT Documents" -Force
Removes the list named ‘IT Documents‘ without asking for confirmation.
Run the code below in Windows Power Shell ISE or Visual Studio Code Editors.
$SiteURL = "https://<tenant>.sharepoint.com/sites/RetailManagementSite"
$docLibInternalName = "ITProjectsDocuments"
$docLibDisplayName = "IT Projects Documents"
# Connect to SharePoint
Connect-PnPOnline -Url $SiteURL -ClientId 23b8efoc-653a-4b7b-af31-0d82f0ea7733 -Interactive
# Check if the document library already exists
$docLib = Get-PnPList -Identity $docLibInternalName -ErrorAction SilentlyContinue
# If the document library exists, delete it
if ($docLib) {
Write-Host "Document library '$docLibDisplayName' already exists. Deleting..." -ForegroundColor Yellow
Remove-PnPList -Identity $docLibInternalName -Force
Write-Host "Document library '$docLibDisplayName' deleted." -ForegroundColor Green
}
# Create the document library
Write-Host "Creating document library '$docLibDisplayName'..." -ForegroundColor Yellow
New-PnPList -Title $docLibInternalName -Template DocumentLibrary -OnQuickLaunch
# Change library display name
Set-PnPList -Identity $docLibInternalName -Title $docLibDisplayName
Write-Host "Document library '$docLibDisplayName' created successfully." -ForegroundColor Green
If the SharePoint document library already exists, it deletes it permanently and creates it again; if it is not yet created, then this code will make it.
Here,
- $SiteURL = Contains the SharePoint site URL.
- $docLibInternalName = SharePoint document library’s internal name.
- $docLibDisplayName = SharePoint document library’s display name.
- In the Connect-PnPOnline command, provide your application ID for the -ClientId parameter.
Now, look at the image below. The SharePoint document library has been created!
Create & Delete Multiple SharePoint Document Libraries Using PnP PowerShell
Let’s see how to delete the SharePoint document library if it already exists and recreating using PnP PowerShell commands.
Run the code below, and you can see Sharepoint document library creation messages in the Terminal pane.
# Variables
$SiteURL = "https://<tenant name>.sharepoint.com/sites/RetailManagementSite"
$DocLibInternalName1 = "InvoicesReceipts"
$DocLibInternalName2 = "PayrollRecords"
$DocLibInternalName3 = "AuditReports"
$DocLibDisplayName1="Invoices Receipts"
$DocLibDisplayName2="Payroll Records"
$DocLibDisplayName3="Audit Reports"
#Creat SharePoint document library
function Create-DocumentLibrary {
param (
[string]$DocumentLibraryName,
[string]$LibraryDisplayName
)
# Create the document library
New-PnPList -Title $DocumentLibraryName -Template DocumentLibrary -OnQuickLaunch
Set-PnPList -Identity $DocumentLibraryName -Title $LibraryDisplayName
Write-Host "Document Library '$LibraryDisplayName' created successfully!" -ForegroundColor Green
}
# delete document library if already existes
function Delete-DocumentLibraryIfExists {
param (
[string]$LibraryDisplayName
)
$library = Get-PnPList -Identity $LibraryDisplayName -ErrorAction SilentlyContinue
if ($library) {
Write-Host "Deleting existing document library '$LibraryDisplayName'..." -ForegroundColor Yellow
Remove-PnPList -Identity $LibraryDisplayName -Force
try {
$recycleBinItems = Get-PnPRecycleBinItem -RowLimit 100 | Where-Object { $_.Title -eq $LibraryDisplayName }
foreach ($item in $recycleBinItems) {
Clear-PnPRecycleBinItem -Identity $item -Force
}
Write-Host "Document library '$LibraryDisplayName' deleted permanently!" -ForegroundColor Green
} catch {
Write-Host "Failed to delete items from the recycle bin. Make sure you have the necessary permissions." -ForegroundColor Red
}
}
}
Connect-PnPOnline -Url $SiteURL -ClientId 23a8ef0c-663a-4b7b-af31-0d82f0ea7733 -Interactive
# Create Document Library1 & delete if already exists
Delete-DocumentLibraryIfExists -LibraryDisplayName $DocLibDisplayName1
Create-DocumentLibrary -DocumentLibraryName $DocLibInternalName1 -LibraryDisplayName $DocLibDisplayName1
# Create Document Library2 & delete if already exists
Delete-DocumentLibraryIfExists -LibraryDisplayName $DocLibDisplayName2
Create-DocumentLibrary -DocumentLibraryName $DocLibInternalName2 -LibraryDisplayName $DocLibDisplayName2
# Create Document Library3 & delete if already exists
Delete-DocumentLibraryIfExists -LibraryDisplayName $DocLibDisplayName3
Create-DocumentLibrary -DocumentLibraryName $DocLibInternalName3 -LibraryDisplayName $DocLibDisplayName3
I have created two functions for creating and deleting the document library here.
- The Create-DocumentLibrary () function will create the SharePoint document library. This function took two parameters,
- DocumentLibraryName = It takes the internal name of the library.
- LibraryDisplayName = It takes the display name of the library.
- The Delete-DocumentLibraryIfExists() function will delete the SharePoint document library if it already exists. This function took only one parameter.
- LibraryDisplayName = Display name of the document library.
This way, we can delete a SharePoint document library if it already exists and create it again.
I hope you understand how to create and delete the SharePoint document library using PnP PowerShell. This article can be followed while trying to create or delete document libraries programmatically, which will be helpful when you’re working with multiple document libraries.
Also, you may like:
- How to create SharePoint list and add multiple columns
- CRUD operations on SharePoint list items using PnP PowerShell
- Create a file if not exist with name as today’s date using PnP PowerShell
- Check file size using PnP PowerShell
- 10 Useful Tips for Customizing SharePoint List Fields with PnP PowerShell
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com