SharePoint Online: Update Choices in the Choice Field using PowerShell

Requirement: Update choice field choices in SharePoint Online using PowerShell.

sharepoint online update choice field using powershell

SharePoint Online: PowerShell to Update Choice Field

As an administrator, you may need to update the choice field in the SharePoint Online list. Updating the choice field’s options can be done through the web browser user interface, but if you want to automate the process, PowerShell provides a way to do that. This post will show you how to use PowerShell to update choices in a choice field.

#Load SharePoint Online 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"
$FieldName="Category"
#Define Choices
$Choices = @("Growth Capital","Private Investment","Development", "Start up", "Real Estate")
  
#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
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()

#Get the field
$Field = $List.Fields.GetByInternalNameOrTitle($FieldName)
$Ctx.Load($Field)
$Ctx.ExecuteQuery()

#Cast the field to Choice Field
$ChoiceField = New-Object Microsoft.SharePoint.Client.FieldMultiChoice($Ctx, $Field.Path)
$Ctx.Load($ChoiceField)
$Ctx.ExecuteQuery()

#$choiceField.Choices.Clear()
$ChoiceField.Choices = $Choices            
$ChoiceField.UpdateAndPushChanges($True)
$Ctx.ExecuteQuery()

Write-host "Choice Field has been Updated!" -ForegroundColor Green

If you want to append to existing choices, use the following:

$ChoiceField.Choices += $Choices  

This adds value to the choice field’s existing choices.

PnP PowerShell to Add Choice to a Choice Column in SharePoint Online

How do you get all the choices of a choice field in the SharePoint Online list?

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName= "Projects"
$FieldInternalName = "Status"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get the Field
$Field = Get-PnPField -Identity $FieldInternalName -List $ListName

#get all choices of the choice field 
$Field.TypedObject.Choices

Now, Let’s update the choice field using PnP PowerShell:

#Parameter
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName= "Projects"
$FieldInternalName = "Status"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
$Ctx = Get-PnPContext

#Get the Field to update
$Field = Get-PnPField -Identity $FieldInternalName -List $ListName

#Cast the field to Choice Field
$ChoiceField = New-Object Microsoft.SharePoint.Client.FieldChoice($Ctx, $Field.Path)
$Ctx.Load($ChoiceField)
Invoke-PnPQuery

#Add a Choice Value
$ChoiceField.Choices += "Dropped"
$ChoiceField.UpdateAndPushChanges($True)
Invoke-PnPQuery

Here is another post on getting and setting choice field values using PowerShell: How to Get/Set Choice Field Values in SharePoint Online using PowerShell?

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!

One thought on “SharePoint Online: Update Choices in the Choice Field using PowerShell

  • How can i delete Values in the Choices?

    Thanks

    Reply

Leave a Reply

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