Adding Columns to a SharePoint List using PnP PowerShell

Loading

In SharePoint Online, columns (also known as fields) help organize and store data within lists. Using PnP PowerShell, you can efficiently add columns to a SharePoint list, making it easier to manage structured data.

This guide covers:
How to connect to SharePoint Online
How to add different column types to a SharePoint list
How to set column properties such as required, indexed, and default values


Prerequisites

Before adding columns, ensure:
You have SharePoint Admin or Site Owner permissions.
You have PnP PowerShell installed.
You know the SharePoint site URL and list name where you want to add columns.


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

To add a column, first, connect to the 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 where the list exists.

Connected successfully!


Step 3: Add a Column to a SharePoint List

To add a column, use the Add-PnPField command.

1️⃣ Add a Text Column

# Add a single-line text column
Add-PnPField -List "Employee Directory" -DisplayName "Employee ID" -InternalName "EmployeeID" -Type Text

🔹 "Employee Directory" → The list name
🔹 "Employee ID" → Column name (as displayed)
🔹 "EmployeeID" → Internal name used in queries
🔹 -Type Text → Single-line text column

Column added successfully!


2️⃣ Add a Number Column

# Add a number column
Add-PnPField -List "Employee Directory" -DisplayName "Salary" -InternalName "Salary" -Type Number

Stores numeric values


3️⃣ Add a Choice Column

# Add a choice column with predefined values
Add-PnPField -List "Employee Directory" -DisplayName "Department" -InternalName "Department" -Type Choice -AddToDefaultView -Choices "HR", "IT", "Finance", "Marketing"

🔹 -Choices → Defines available options
🔹 -AddToDefaultView → Adds column to the default view

Allows users to pick from predefined values


4️⃣ Add a Date Column

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

Stores date values


5️⃣ Add a Yes/No (Boolean) Column

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

Stores TRUE/FALSE values


6️⃣ Add a Lookup Column

# Add a lookup column referencing another list
Add-PnPField -List "Employee Directory" -DisplayName "Manager" -InternalName "Manager" -Type Lookup -LookupList "Managers" -LookupField "FullName"

🔹 -LookupList → The referenced list
🔹 -LookupField → The field to pull data from

Links data from another list


7️⃣ Add a Person or Group Column

# Add a person column
Add-PnPField -List "Employee Directory" -DisplayName "Assigned To" -InternalName "AssignedTo" -Type User

Stores users/groups from Azure AD


Step 4: Verify the Column

After adding the column, verify it using:

# Get all columns in the list
Get-PnPField -List "Employee Directory" | Select Title, InternalName, TypeAsString

This lists all columns in the SharePoint list.


Step 5: Set Column Properties (Optional)

Make a Column Required

# Set a column as required
Set-PnPField -List "Employee Directory" -Identity "EmployeeID" -Required $true

Users must fill this column


Enable Column Indexing

# Enable indexing for better performance
Set-PnPField -List "Employee Directory" -Identity "EmployeeID" -Indexed $true

Speeds up search queries


Set a Default Value

# Set a default value for a column
Set-PnPField -List "Employee Directory" -Identity "Department" -DefaultValue "IT"

New items will have “IT” as default


Step 6: Remove a Column (If Needed)

To delete a column:

# Remove a column
Remove-PnPField -List "Employee Directory" -Identity "Department" -Force

Column deleted permanently!


Common Errors & Troubleshooting

ErrorCauseSolution
Access DeniedYou don’t have Admin or Site Owner permissionsEnsure you have the right permissions
The term 'Add-PnPField' is not recognizedPnP PowerShell is not installedRun Import-Module PnP.PowerShell
Cannot find list with identity 'Employee Directory'The list does not existVerify list name using Get-PnPList before adding columns

Leave a Reply

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