How to Check If a Field Exists in a SharePoint Online List Using PnP PowerShell?

Recently, I got a requirement to check if a column exists in a SharePoint list. Instead of manually checking, I used PowerShell. In this tutorial, I will show you how to check if a field exists in a SharePoint Online list using PnP PowerShell. The same script will work for a document library also.

Check If a Field Exists in a SharePoint Online List Using PnP PowerShell

Here, I will show you two methods to check if a field exists in a SharePoint Online list using PowerShell.

Here, I have a created a SharePoint Online list named “EmployeeRecords” and we want to check if a field named “EmployeeID” exists. You can see the list and column like below:

Check If a Field Exists in a SharePoint Online List Using PnP PowerShell

Method 1: Using Get-PnPField

The Get-PnPField cmdlet retrieves fields from a specified SharePoint Online list. You can use it to check if a field exists by attempting to retrieve it by name using this PnP PowerShell cmdlets.

Here is the complete PnP PowerShell script.

Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerShellFAQs" -Interactive
$listName = "EmployeeRecords"
$fieldName = "EmployeeID"

try {
    $field = Get-PnPField -List $listName -Identity $fieldName
    if ($field) {
        Write-Output "Field 'EmployeeID' exists in the list 'EmployeeRecords'."
    }
} catch {
    Write-Output "Field 'EmployeeID' does not exist in the list 'EmployeeRecords'."
}

Once you execute the above PowerShell script, you can see it displaying the exact output in the screenshot below. Since the column exists, it displays a message as “Field ‘EmployeeID’ exists in the list ‘EmployeeRecords’.”.

pnp powershell check if field exists in sharepoint list

This is one of the best method to check if a field exists in SharePoint Online list using PnP PowerShell.

Check out Get All List Fields in SharePoint Online using PnP PowerShell

Method 2: Using Get-PnPList and Fields Property

Let me show you another method to check if a field exists in SharePoint Online list. I will show you how to use the Get-PnPList cmdlet and Fields Property to check if the field exists in the SharePoint Online list.

Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerShellFAQs" -Interactive
$listName = "EmployeeRecords"
$fieldName = "EmployeeID"

# Get the list
$list = Get-PnPList -Identity $listName

# Load the fields collection
$ctx = Get-PnPContext
$ctx.Load($list.Fields)
$ctx.ExecuteQuery()

# Check if the field exists
$fieldExists = $list.Fields | Where-Object { $_.InternalName -eq $fieldName }

if ($fieldExists) {
    Write-Output "Field 'EmployeeID' exists in the list 'EmployeeRecords'."
} else {
    Write-Output "Field 'EmployeeID' does not exist in the list 'EmployeeRecords'."
}

This script retrieves the SharePoint Online list and checks its Fields collection to see if a field with the specified internal name exists. I executed the above PnP PowerShell script and you can see the output in the screenshot below:

pnp powershell check if column exists in sharepoint online list

Conclusion

Using PnP PowerShell to check if a field exists in a SharePoint Online list can be done efficiently with the Get-PnPField cmdlet or by examining the Fields property of a list. I highly recommend to use the Get-PnPField PnP PowerShell to check if a field exists in a SharePoint Online List.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.