SharePoint Online: Add Indexed Column using PowerShell

Requirement: Create Indexed column SharePoint Online.

How to Add Indexed Column in SharePoint Online?

Indexed columns add performance benefits in large lists when you frequently filter and query a column. To create an indexed column SharePoint Online, follow these steps:

  • Navigate to the List or Library >> List Settings
  • Scroll down to the Columns section >> Click on Indexed columns.
    indexed column in sharepoint online
  • On the Indexed Columns page, click on “Create a new index”. 
    sharepoint online create indexed column
  • Select a column from the drop-down in the Primary Column section. Click on Create. This creates index on the list. You can create up to 20 indexed columns in any SharePoint list or library.
    poweshell to create indexed column sharepoint online

Please note, not every column type can be indexed, Such as: Multiple lines of text, Multi-valued Choice, Lookup, Hyperlink or Picture, calculated columns, Custom Columns, Multi-valued Person or Group, External data.

Auto-Indexing feature in SharePoint automatically creates appropriate indices when your list approaches 5,000 items!

SharePoint Online: Create Indexed Column using PowerShell:

Here is the PowerShell to create an indexed column

#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"
#Variables for Processing
$SiteUrl = ""
$ColumnName="Project Name" #Display name of the field

Try { 
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    #Set up the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = $credentials
    #Get the List and column to add index
    $List = $Ctx.web.Lists.GetByTitle($ListName)
    $Field = $List.Fields.GetByTitle($ColumnName)
    $Field.Indexed = $True

    Write-Host -f Green "Column Index Added Successfully!"
Catch {
    write-host -f Red "Error Adding Indexed to Column!" $_.Exception.Message

Once you execute this PowerShell script, You should see “Project Name” field in indexed column.

Remove Indexed Column from SharePoint Online list using PowerShell

To remove the index from the column, set “Indexed” property to “False”

$Field.Indexed = $False

PnP PowerShell to Create Index column in SharePoint Online

#Config Variables
$SiteURL = ""
$ListName = "Projects"
$ColumnName = "ProjectName" #Internal Name

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Context
$Context = Get-PnPContext

#Get the Field from List
$Field = Get-PnPField -List $ListName -Identity $ColumnName

#Set the Indexed Property of the Field
$Field.Indexed = $True

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply