![]()
In this “List operations using PnP PowerShell” article, we will learn about various list operations in SharePoint Online using the PnP PowerShell script, like how to add a column, how to get column details, and how to delete a column using the PnP PowerShell script in the SharePoint Online list. Before getting into the Power Script, let us understand what is PnP PowerShell.
What is PnP PowerShell?
PnP PowerShell is a powerful automation tool designed to simplify and enhance the management of Microsoft 365 and SharePoint environments. Developed by the SharePoint Patterns and Practices (PnP) community, it offers a comprehensive set of cmdlets tailored for both administrators and developers. These cmdlets cover a wide range of tasks, from site provisioning and configuration to data migration and governance.
Key Features
- Comprehensive Command Set
- PnP PowerShell includes hundreds of cmdlets that enable automation of almost all aspects of SharePoint Online, Microsoft Teams, and other Microsoft 365 services. Common tasks include site collection management, document library operations, and user permissions handling.
- Cross-Platform Support
- PnP PowerShell is built on .NET Core, making it compatible with multiple operating systems, including Windows, macOS, and Linux. This flexibility allows users to manage their Microsoft 365 environments from virtually any device.
- Ease of Use
- Designed with simplicity in mind, PnP PowerShell cmdlets often reduce the complexity of tasks. For example, provisioning a new SharePoint site with custom templates and settings can be accomplished with just a few lines of code.
- Community-Driven
- As an open-source project, PnP PowerShell benefits from continuous contributions and updates from the SharePoint community. This ensures the tool remains up-to-date with the latest Microsoft 365 features and best practices.
- Extensive Documentation
- PnP PowerShell comes with detailed documentation and numerous examples, making it easier for users to get started and find solutions to specific problems.
Common Use Cases
- Site Provisioning
- Automate the creation of SharePoint sites, including setting up templates, applying branding, and configuring settings.
- Content Migration
- Move content between SharePoint sites or from on-premises to online environments efficiently and accurately.
- User and Permission Management
- Manage user access, groups, and permissions across your Microsoft 365 environment to ensure proper security and compliance.
- Bulk Operations
- Perform bulk operations such as updating metadata for thousands of documents or creating multiple sites in one go.
- Reporting and Monitoring
- Generate reports on site usage, document activity, and user permissions to keep track of your environment’s health and compliance.
Getting Started with PnP PowerShell
- Installation
- Install PnP PowerShell using the PowerShell Gallery. The basic command for installation is:
powershell
Install-Module -Name PnP.PowerShell
- Install PnP PowerShell using the PowerShell Gallery. The basic command for installation is:
- Authentication
- Authenticate with your Microsoft 365 environment using:
powershell
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com" -Interactive
- Authenticate with your Microsoft 365 environment using:
- Executing Commands
- Start using PnP PowerShell by executing commands. For example, to retrieve all lists in a site, you can use:
powershell
Get-PnPList
- Start using PnP PowerShell by executing commands. For example, to retrieve all lists in a site, you can use:
- Learning Resources
- Utilize the PnP PowerShell documentation for detailed guidance and examples.
How to add a column in SharePoint online list using PnP PowerShell?
Using the below PnP PowerShell code we can add the column to the SharePoint Online list. The “Add-PnPField” command is used to create a column in the SharePoint list.
Lists operations using PnP PowerShell
#Create list column using the PnP PowerShell Add-PnPField -DisplayName "PnPLTestColumn" -InternalName "PnPLTestColumn" -Group "Custom Columns" -Type Text -List $listName -AddToDefaultView -Required #Create list column using the PnP PowerShell - ends here
How to get column property in SharePoint online list using PnP PowerShell?
Using the below PnP PowerShell code, we can get the column property for the given column. Here the “Get-PnPField” command is responsible for getting the property of the column. In the below example I have used the “Title” column as the key column – meaning bypassing the “Title” column as the identity parameter, we get all properties of the “Title” column.
#The below codes getting the column property of Title column. $myColumn = Get-PnPField -List $listName -Identity "Title" Write-Host "Description :" $myColumn.Description Write-Host "Group Name :" $myColumn.Group Write-Host "Internal Name :" $myColumn.InternalName Write-Host "Static Name :" $myColumn.StaticName Write-Host "Scope :" $myColumn.Scope Write-Host "Type :" $myColumn.TypeDisplayName Write-Host "Schema XML :" $myColumn.SchemaXml Write-Host "Is Required? :" $myColumn.Required Write-Host "Is read only? :" $myColumn.ReadOnlyField Write-Host "Unique? :" $myColumn.EnforceUniqueValues Write-Host "----------------------------------" #The below codes getting the column property of Title column - ends here.
How to get all column properties in SharePoint online list using PnP PowerShell?
Using the below PnP PowerShell we will get all column properties in SharePoint online list – it is just the same as the above command “Get-PnPField” which only takes the list as a parameter, then it loops through all the columns for the given list.
#The below codes generate all column properties from the given list.
$allColumns = Get-PnPField -List $listName
foreach($oneColumn in $allColumns)
{
Write-Host "Column Title :" $oneColumn.Title
Write-Host "Description :" $oneColumn.Description
Write-Host "Group Name :" $oneColumn.Group
Write-Host "Internal Name :" $oneColumn.InternalName
Write-Host "Static Name :" $oneColumn.StaticName
Write-Host "Scope :" $oneColumn.Scope
Write-Host "Type :" $oneColumn.TypeDisplayName
Write-Host "Schema XML :" $oneColumn.SchemaXml
Write-Host "Is Required? :" $oneColumn.Required
Write-Host "Is read only? :" $oneColumn.ReadOnlyField
Write-Host "Unique? :" $oneColumn.EnforceUniqueValues
Write-Host "---------------------------------"
}
How to delete a column in SharePoint online list using the PnP PowerShell?
Using the below PnP PowerShell command we can delete a column from the SharePoint Online list. The “Remove-SPOField” is used to delete the column from the list which takes the list as the list name and identity as the column name parameter.
#The below code removes the particular list column from the given list. Remove-SPOField -List $listName -Identity "PnPLTestColumn" -Force #The below code removes the particular list column from the given list - ends here
List Operations in SharePoint Online using PnP PowerShell – complete code
Below is the complete code for the list operations in SharePoint Online using the PnP PowerShell.
####The below script is used to do the PnP list operations like Create, Read, Delete columns in the SharePoint online list.
cls
$PSshell = Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorVariable err -ErrorAction SilentlyContinue
if($PSshell -eq $null)
{
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$fileName = "List_Operation_using_PnP_PowerShell"
#'yyyyMMddhhmm yyyyMMdd
$enddate = (Get-Date).tostring("yyyyMMddhhmmss")
#$filename = $enddate + '_VMReport.doc'
$logFileName = $fileName +"_"+ $enddate+"_Log.txt"
$invocation = (Get-Variable MyInvocation).Value
$directoryPath = Split-Path $invocation.MyCommand.Path
$directoryPathForLog=$directoryPath+"\"+"LogFiles"
if(!(Test-Path -path $directoryPathForLog))
{
New-Item -ItemType directory -Path $directoryPathForLog
#Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}
#$logPath = $directoryPath + "\" + $logFileName
$logPath = $directoryPathForLog + "\" + $logFileName
$isLogFileCreated = $False
function Write-Log([string]$logMsg)
{
if(!$isLogFileCreated){
Write-Host "Creating Log File..."
if(!(Test-Path -path $directoryPath))
{
Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}
else
{
$script:isLogFileCreated = $True
Write-Host "Log File ($logFileName) Created..."
[string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
Add-Content -Path $logPath -Value $logMessage
}
}
else
{
[string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
Add-Content -Path $logPath -Value $logMessage
}
}
#Paramaters area
$adminSiteURL="globalsharepoint2020-admin.sharepoint.com" //This is your admin site URL.
$SiteURL="globalsharepoint2020.sharepoint.com/"
$userName = "YourSPOUser@YourSPOTenant.sharepoint.com"
$passWord = "YourSPOPassword"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
$listName="Employee"
#Paramaters area - Ends
try
{
Connect-PnPOnline -Url $SiteURL -Credentials $cred
#Create list column using the PnP PowerShell
Add-PnPField -DisplayName "PnPLTestColumn" -InternalName "PnPLTestColumn" -Group "Custom Columns" -Type Text -List $listName -AddToDefaultView -Required
#Create list column using the PnP PowerShell - ends here
#The below codes getting the column property of the Title column.
$myColumn = Get-PnPField -List $listName -Identity "Title"
Write-Host "Description :" $myColumn.Description
Write-Host "Group Name :" $myColumn.Group
Write-Host "Internal Name :" $myColumn.InternalName
Write-Host "Static Name :" $myColumn.StaticName
Write-Host "Scope :" $myColumn.Scope
Write-Host "Type :" $myColumn.TypeDisplayName
Write-Host "Schema XML :" $myColumn.SchemaXml
Write-Host "Is Required? :" $myColumn.Required
Write-Host "Is read only? :" $myColumn.ReadOnlyField
Write-Host "Unique? :" $myColumn.EnforceUniqueValues
Write-Host "----------------------------------"
#The below codes getting the column property of Title column - ends here.
#The below codes generate all columns properties from the given list.
$allColumns = Get-PnPField -List $listName
foreach($oneColumn in $allColumns)
{
Write-Host "Column Title :" $oneColumn.Title
Write-Host "Description :" $oneColumn.Description
Write-Host "Group Name :" $oneColumn.Group
Write-Host "Internal Name :" $oneColumn.InternalName
Write-Host "Static Name :" $oneColumn.StaticName
Write-Host "Scope :" $oneColumn.Scope
Write-Host "Type :" $oneColumn.TypeDisplayName
Write-Host "Schema XML :" $oneColumn.SchemaXml
Write-Host "Is Required? :" $oneColumn.Required
Write-Host "Is read only? :" $oneColumn.ReadOnlyField
Write-Host "Unique? :" $oneColumn.EnforceUniqueValues
Write-Host "---------------------------------"
}
#The below code removes the particular list column from the given list.
Remove-SPOField -List $listName -Identity "PnPLTestColumn" -Force
#The below code removes the particular list column from the given list - ends here
}
catch
{
$ErrorMessage = $_.Exception.Message +"in PnP list operation Create, Read, Delete columns!:"
Write-Host $ErrorMessage -BackgroundColor Red
Write-Log $ErrorMessage
}
Summary: List operations using PnP PowerShell
Thus, in this article we have learned the below with respect to list operations in SharePoint Online using the PnP PowerShell:
- How to add a column in SharePoint online list using PnP PowerShell?
- How to get column property in SharePoint online list using PnP PowerShell?
- How to get all column properties in SharePoint online list using PnP PowerShell?
- How to delete a column in SharePoint online list using the PnP PowerShell?
- How to remove a column from the SharePoint Online list using PnP PowerShell?
- List operations in SharePoint Online using PnP PowerShell – complete code
PnP PowerShell is an essential tool for anyone managing Microsoft 365 and SharePoint environments. Its comprehensive set of cmdlets, cross-platform support, and ease of use make it a powerful solution for automating complex tasks and enhancing operational efficiency. By leveraging PnP PowerShell, administrators and developers can streamline their workflows, ensure compliance, and focus on delivering value to their organizations.
See Also: SharePoint PowerShell Tutorials
You may also like the following SharePoint PowerShell tutorials:
- [Verified]: Find all InfoPath forms in SharePoint using PowerShell
- [Verified]: Remove recycle bin items in SharePoint using PowerShell
- [Verified]: Cancel SharePoint workflows using PowerShell
- SharePoint Online: Remove custom app using PnP PowerShell
- Office 365: How to create content type in SharePoint Online using PowerShell?
- Office 365: How to create document library in SharePoint Online using PowerShell?
- Export SharePoint user information list to CSV(Excel) file using PowerShell
- How to fix “The term ‘Get-MsolUser’ is not recognized as the name of a cmdlet”
- How to fix the “The term ‘Get-SPWeb’ is not recognized as the name of a cmdlet, function” PowerShell error
- How to hide quick launch menu in SharePoint online using PnP PowerShell
- Edit user Permission is greyed Out SharePoint Online
- Get workflow inventory from SharePoint online using PowerShell CSOM
- Create a modern team site using PnP PowerShell in SharePoint
- In 2 steps convert a classic SharePoint page to modern using PnP
- SharePoint Online: Delete All Files from document library for the given date – PowerShell CSOM
- Create SharePoint online list using PnP provisioning template
- SharePoint Automation: PowerShell script to get remote server information
- Office 365: Retrieve hub sites and associated sites using PnP Powershell
- SharePoint Online Automation – O365 – Upload files to document library using PowerShell CSOM
- SharePoint Online Automation – O365 – Create multiple items in a list using PowerShell CSOM
- SharePoint Online Automation – O365 – Update document library metadata using PowerShell CSOM
- PnP PowerShell overview