Creating a SharePoint List using PnP PowerShell

Loading

A SharePoint List is a powerful tool that allows you to store and manage data in SharePoint Online. You can create lists manually through the SharePoint UI, but for efficiency and automation, you can use PnP PowerShell to create SharePoint lists programmatically.

In this guide, we will cover:
How to connect to SharePoint Online
How to create a custom SharePoint list
How to add columns (fields) to the list
How to verify the list creation


Prerequisites

Before creating a list, ensure that:

✔️ You have SharePoint Admin or Site Owner permissions.
✔️ You have PnP PowerShell installed on your system.
✔️ You know the SharePoint site URL where you want to create the list.


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

Step 2: Connect to SharePoint Online

To create a list, you first need to connect to the SharePoint site where the list will be created.

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

🔹 Replace "yourtenant" with your actual tenant name.
🔹 Replace "yoursite" with the target site name.

For app-based authentication, use:

Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -ClientId "Your-App-Client-ID" -Tenant "yourtenant.onmicrosoft.com" -CertificatePath "Path\To\Certificate.pfx"

Step 3: Create a SharePoint List

Use the New-PnPList command to create a list.

# Create a new SharePoint List
New-PnPList -Title "Employee Directory" -Template GenericList -Url "EmployeeDirectory" -EnableContentTypes $true -OnQuickLaunch

🔹 Parameters Explained:

  • -Title "Employee Directory" → Name of the list
  • -Template GenericList → Creates a Custom List (Other options: DocumentLibrary, Tasks, Announcements, etc.)
  • -Url "EmployeeDirectory" → Internal name of the list (avoid spaces)
  • -EnableContentTypes $true → Allows enabling content types
  • -OnQuickLaunch → Adds the list to the Quick Launch menu

List created successfully!


Step 4: Add Columns (Fields) to the List

After creating the list, add columns using Add-PnPField.

# Add Single Line of Text column
Add-PnPField -List "Employee Directory" -DisplayName "Employee Name" -InternalName "EmployeeName" -Type Text

# Add Number column
Add-PnPField -List "Employee Directory" -DisplayName "Employee ID" -InternalName "EmployeeID" -Type Number

# Add Choice column
Add-PnPField -List "Employee Directory" -DisplayName "Department" -InternalName "Department" -Type Choice -Choices "HR", "IT", "Finance", "Marketing"

# Add Date column
Add-PnPField -List "Employee Directory" -DisplayName "Joining Date" -InternalName "JoiningDate" -Type DateTime

# Add Yes/No column
Add-PnPField -List "Employee Directory" -DisplayName "Active Employee" -InternalName "ActiveEmployee" -Type Boolean

Column Types Explained:

  • "Text" → Single line of text
  • "Number" → Numeric values
  • "Choice" → Dropdown with multiple choices
  • "DateTime" → Date selection
  • "Boolean" → Yes/No (true/false)

Columns added successfully!


Step 5: Verify the List and Columns

Option 1: Using PowerShell

Run the following command to verify list details:

Get-PnPList -Identity "Employee Directory"

Run this command to check all fields:

Get-PnPField -List "Employee Directory"

Option 2: Using SharePoint UI

1️⃣ Go to SharePoint Online
2️⃣ Navigate to your target site
3️⃣ Click on Site Contents
4️⃣ Open Employee Directory and check the columns

Verification complete!


Step 6: Delete a SharePoint List (If Needed)

If you need to remove the list, use:

powershellCopyEditRemove-PnPList -Identity "Employee Directory" -Force

Common Errors & Troubleshooting

ErrorCauseSolution
New-PnPList : Access DeniedYou don’t have Site Owner/Admin permissionsEnsure you are a Site Owner or SharePoint Admin
The term 'New-PnPList' is not recognizedPnP PowerShell is not installed or importedRun Import-Module PnP.PowerShell
Cannot connect to SharePoint OnlineAuthentication issueUse -Interactive login or App-based authentication
Column type is invalidIncorrect field type in Add-PnPFieldUse correct types (Text, Number, Choice, DateTime, Boolean)

Leave a Reply

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