How to Add a Column to List in SharePoint Online Using PowerShell?

Requirement: Add a Column to a List in SharePoint Online with PowerShell.

How to Add a Column to SharePoint Online List or Library?

SharePoint Online Lists are used to store data, and each list can have multiple columns. Columns define the type of data that can be stored in the list. For example, a list of employees may have columns for their name, department, and job title. Adding a column to a list in SharePoint Online is a straightforward process.

Here’s a step-by-step guide on how to add a new column to a SharePoint Online list using the web interface:

  1. Navigate to your SharePoint Online site and go to the desired list where you want to add the new column. You can access the list by clicking on its name in the site’s navigation or by going to Site Contents and selecting the list.
  2. Once you are on the list view, click on the “+ Add column” button, which is located in the header of the list view.
    add column to sharepoint online list using powershell
  3. A pop-up page will appear, offering several column types to choose from, such as:
    sharepoint online powershell add field to list
    • Single line of text
    • Multiple lines of text
    • Choice (drop-down menu)
    • Number
    • Currency
    • Date and Time
    • Lookup (reference to another list)
    • Yes/No (checkbox)
    • Person or Group
    • Hyperlink or Picture
  4. Select the desired column type, and a new configuration panel will appear on the right side of the screen.
  5. In the configuration panel, provide the following information:
    • Name: Enter a name for the new column.
    • Description: Optionally, provide a description to help users understand the purpose of the column.
    • Type-specific settings: Depending on the column type you selected, there may be additional configuration options available, such as choices for a drop-down menu, number formatting, or date and time formats.
  6. After you have configured the new column, click the “Save” button at the bottom of the configuration panel.
    sharepoint online add column to list
  7. The new column will now appear in the list view, and you can start using it to store and display additional information.

That’s it! Using the web interface, you have successfully added a new column to a SharePoint Online list. Remember that the column’s settings can be adjusted later if needed by going to the list settings page and selecting the column’s name.

Add a Column to the list using the PowerShell CSOM script

With the help of PowerShell, you can streamline your SharePoint Online workflow by automating repetitive tasks, such as adding a column to a list. Let me guide you through the steps to add a column to a list using PowerShell.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set parameters
$SiteURL="https://salaudeen.sharepoint.com/sites/Retail"
$ListName="Projects"
$Name="ProjectCode"
$DisplayName="Project Code"
$Description="Enter the Project Code"
$IsRequired = "TRUE"
$EnforceUniqueValues="FALSE" #Must set Indexed="FALSE", if Unique="TRUE"
$MaxLength="100"
 
#Generate new GUID for Field ID
$FieldID = New-Guid
 
Try {
    #Get Credentials to connect
    $Cred= Get-Credential
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
         
    #Get the List
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()
 
    #Check if the column exists in list already
    $Fields = $List.Fields
    $Ctx.Load($Fields)
    $Ctx.executeQuery()
    $NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
    if($NewField -ne $NULL)  
    {
        Write-host "Column $Name already exists in the List!" -f Yellow
    }
    else
    {
        #Define XML for Field Schema
        $FieldSchema = "<Field Type='Text' ID='{$FieldID}' Name='$Name' StaticName='$Name' DisplayName='$DisplayName' Description='$Description' Required='$IsRequired' EnforceUniqueValues='$EnforceUniqueValues' MaxLength='$MaxLength' />"
        $NewField = $List.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
        $Ctx.ExecuteQuery()    
 
        Write-host "New Column Added to the List Successfully!" -ForegroundColor Green  
    }
}
Catch {
    write-host -f Red "Error Adding Column to List!" $_.Exception.Message
}

This can be especially useful if you need to add multiple fields or if you want to automate the process of adding fields to lists.

PnP PowerShell to Add a Column to SharePoint Online List

If you prefer to add a column to a list programmatically in SharePoint Online, use this PnP PowerShell script. Ensure that you have the PnP.PowerShel Module installed on your computer before running this script. Use the Add-PnPField cmdlet to add a new column to your SharePoint Online list. This cmdlet requires you to specify several parameters, such as the column name, column type, and display name.

#Parameters
$SiteURL = "https://Crescent.sharepoint.com/sites/Projects"
$ListName= "Team Projects"
$FieldTitle= "Project ID"
$FieldInternalName ="ProjectID"
$FieldType = "Text"
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
     
    #Add new field to list
    Add-PnPField -List $ListName -DisplayName $FieldTitle -InternalName $FieldInternalName -Type $FieldType -Required -AddToDefaultView -ErrorAction Stop
    Write-host -f Green "New Field '$FieldTitle' Added to the List!" 
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Make sure you replace the parameters per your requirements. Similarly, You can add a field to the SharePoint Online list by its schema:

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/retail"
$ListName= "Projects"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Define XML Schema for Text Field
$FieldXML= "<Field Type='Text' Name='ProjectCode' ID='$([GUID]::NewGuid())' DisplayName='Project Code' Required ='TRUE' EnforceUniqueValues = 'TRUE' Indexed='TRUE'></Field>"
 
#Add a Field to list from XML schema
Add-PnPFieldFromXml -FieldXml $FieldXML -List $ListName

Refer to this Microsoft article for all available field schemas: https://learn.microsoft.com/en-us/sharepoint/dev/schema/field-element-field

How about adding other field types?

Here is my list of posts on adding other field types to the SharePoint Online list using PowerShell:

Wrapping up

In this article, we walked through the process of adding a column to a list in SharePoint Online using PowerShell. By leveraging the power of the SharePoint Online CSOM and PnP PowerShell, you can automate and streamline the process of adding your SharePoint Online list columns. In conclusion, adding a column to a list in SharePoint Online using PowerShell is a simple process that can save time and reduce errors in your workflow.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

Leave a Reply

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