Creating a Document Library using PnP PowerShell

Loading

A Document Library in SharePoint Online is a secure place to store, organize, share, and access files. Using PnP PowerShell, you can quickly create document libraries with customized settings.

What You’ll Learn:

✔️ How to connect to SharePoint Online
✔️ How to create a document library using PnP PowerShell
✔️ How to configure document library settings
✔️ How to verify the creation


Prerequisites

Before proceeding, ensure that:
PnP PowerShell is installed
You have the necessary SharePoint permissions
You know the SharePoint site URL


Step 1: Install and Import PnP PowerShell

If you haven’t installed PnP PowerShell, install it using:

Install-Module -Name PnP.PowerShell -Scope CurrentUser -AllowClobber -Force

Then, import the module:

Import-Module PnP.PowerShell

PnP PowerShell is ready!


Step 2: Connect to SharePoint Online

Before making changes, connect to your SharePoint site:

# Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive

🔹 Replace "yourtenant" with your tenant name
🔹 Replace "yoursite" with the site name

Connected successfully!


Step 3: Create a New Document Library

To create a document library, run:

# Define the document library name and description
$libraryName = "Project Documents"
$libraryDescription = "Library for storing project-related files"

# Create the document library
New-PnPList -Title $libraryName -Template DocumentLibrary -Description $libraryDescription -EnableVersioning $true

🔹 This creates a document library named "Project Documents" with versioning enabled.

Document Library Created!


Step 4: Configure Additional Library Settings

After creation, you may want to customize settings.

Enable Major & Minor Versioning

Set-PnPList -Identity $libraryName -EnableVersioning $true -EnableMinorVersions $true -MajorVersions 50 -MinorVersions 10

🔹 Keeps 50 major versions and 10 minor versions.

Versioning configured!

Enable Content Approval

Set-PnPList -Identity $libraryName -EnableModeration $true

🔹 Requires files to be approved before becoming visible.

Content approval enabled!

Change the Library’s Document Template

Set-PnPList -Identity $libraryName -DocumentTemplate "docx"

🔹 Sets the default template to Word documents.

Document template updated!


Step 5: Verify the Document Library

To confirm the library was created, run:

Get-PnPList -Identity $libraryName | Select-Object Title, BaseType, EnableVersioning, EnableMinorVersions

Library verification successful!


Step 6: Delete the Document Library (If Needed)

If you need to remove the document library, run:

Remove-PnPList -Identity $libraryName -Force

🔹 Deletes the document library permanently.

Library deleted!


Common Errors & Solutions

ErrorCauseSolution
Access DeniedInsufficient permissionsEnsure you have Full Control on the site
Cannot find listIncorrect library nameUse Get-PnPList to verify the name
Library already existsDuplicate nameUse a unique name or delete the existing library

Leave a Reply

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