SharePoint Online: Add Index Column using PowerShell

Requirement: Create an Indexed column in SharePoint Online.

How to Add Indexed Columns in SharePoint Online?

Indexed columns add performance benefits in large lists when you frequently filter and query a column. An indexed column in SharePoint Online is a special type of column used to store values that can be used as search keys. Indexed columns enable users to quickly find and retrieve information by using search and filter functions.

To create an indexed column in SharePoint Online, follow these steps:

  1. Navigate to the List or Library >> Click on Settings >> Choose List Settings.
  2. Scroll down to the Columns section >> Click on Indexed columns.
    indexed column in sharepoint online
  3. On the Indexed Columns page, click on “Create a new index”. 
    sharepoint online create indexed column
  4. Select a column from the drop-down in the Primary Column section. Click on Create. This creates an 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, and External data.

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

SharePoint Online: Create an Indexed Column using PowerShell

Here is the PowerShell to create an indexed column, to improve the performance of search queries on a list or library.

#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 = "https://crescent.sharepoint.com/"
$ListName="Projects"
$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
    $Field.Update()
    $Ctx.ExecuteQuery()

    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 the “Project Name” field in the indexed column.

Remove Indexed Column from SharePoint Online list using PowerShell

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

$Field.Indexed = $False

PnP PowerShell to Create Index column in SharePoint Online

Let’s look at using the PowerShell script to create an index column in the SharePoint Online list or library.

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$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
$Field.Update() 
$Context.ExecuteQuery() 

Creating an index column in SharePoint Online is a simple and straightforward process that can greatly improve the efficiency of your information management. By using an index column, users can quickly and easily search and filter their data.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

3 thoughts on “SharePoint Online: Add Index Column using PowerShell

  • It works perfectly, my question is how can I add to columns in the script?

    Reply
  • For large list :

    $field = Get-PnPField -List -Identity ”
    $field.EnableIndex()
    $field.Context.ExecuteQuery()

    Reply
  • Do Indexes also increase the performance for web applications, like Power Platform Canvas App or Power Automate, that use odata queries to search for data in SharePoint online?

    Reply

Leave a Reply

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