PowerShell Scripts to Manage Choice Field in SharePoint

PowerShell is a handy way to perform tedious and repetitive tasks in SharePoint. Say, we want to add or remove a choice value in a specific choice column for number of sites, etc.

Here are some nifty PowerShell scripts to manage choice field in SharePoint.

sharepoint powershell choice field

How to Add New Choice Field to SharePoint List using PowerShell?

Let’s use PowerShell to create choice column:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Define URL and List name variables 
$WebURL="https://sharepoint.crescent.com"
$ListName ="Employee Profiles"

#Get the Web and Lists objects
$web = Get-SPWeb $WebURL
$List = $web.Lists[$ListName]

#sharepoint powershell to add choice field
$list.Fields.Add("Department", [Microsoft.SharePoint.SPFieldType]::Choice, $false)

#Add Choices to the field
$ChoiceField = $list.Fields.GetField("Department") #Get the field
$ChoiceField.Choices.Add("Sales")
$ChoiceField.Choices.Add("Marketing")

#Commit changes
$ChoiceField.update()

This PowerShell adds choice field.

SharePoint PowerShell to Add Choices to Choice Field

Choices can be added from arrays also. Here is how to add choices to the choice field using PowerShell

$ChoiceField = $list.Fields.GetField("Department") #Get the field
#Add in bulk - Array as Choices
$ChoicesArray = @("Marketing","Production","Operations","HR","IT")
$ChoiceField.choices.addrange($ChoicesArray)

#Commit changes
$ChoiceField.update()

PowerShell to create choice column – Multi-Choice Field (Checkboxes)

#Choices for the column
$Choices = New-Object System.Collections.Specialized.StringCollection;
$Choices.Add("Sales")
$Choices.Add("Purchase")
$Choices.Add("Operations")

#Add Multichoice field
$list.Fields.Add("Department",[Microsoft.SharePoint.SPFieldType]::MultiChoice,$False,$False,$Choices)
New fields added to SharePoint lists will not be included in default view by default! You have to add them explicitly.

Remove choices from the choice field:

#Get the choice field
$ChoiceField = $list.Fields.GetField("Department")

#Get all choices: 
$ChoiceField.Choices

#Remove a Choice from the column
$ChoiceField.Choices.Remove("Sales")
#To remove all choices, use: 
#$ChoiceField.Choices.Clear()

#Commit changes to update choice column
$ChoiceField.update()

Set Choice fields settings – SharePoint PowerShell to update choice field

We’ve added the choice field. Now, we can configure choice field settings via PowerShell.

#Get the Web and Lists objects
$web = Get-SPWeb $WebURL
$List = $web.Lists[$ListName]

#Get the choice field
$ChoiceField = $list.Fields.GetField("Department")

#Required Field?
$ChoiceField.Required = $true
#Set Default value
$ChoiceField.DefaultValue = "Operations"
#Allow Fill-in Choices
$ChoiceField.FillInChoice = $false
#Field format: such as radio button, drop down list
$ChoiceField.EditFormat = "RadioButtons"

#Commit changes
$ChoiceField.update()

You may get error when the column is “Sealed”. Set it to “False” to perform any of the change/delete operations.

To check if a choice field is a multiple choice, use:  SPField.FieldValueType.Name -eq “SPFieldMultiChoiceValue”

For all supported properties of the choice field in SharePoint, refer SPFieldChoice reference in MSDN. To get choice field value or set choice field value using PowerShell, refer to How to Get or Set Choice field values using PowerShell?

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

4 thoughts on “PowerShell Scripts to Manage Choice Field in SharePoint

  • Can I Change Choice Field Type from Radio button to Check Boxes?

    Reply
    • Sure, Here is the Script:
      $Field = $List.Fields.GetFieldByInternalName($FieldInternalName)
      $Field.Type = [Microsoft.SharePoint.SPFieldType]::MultiChoice
      $Field.Update($true)

      Reply
  • Hello,

    Please Sir, if I want to have a drop down list of choice and the field must be Required! what should I modify here :

    $list.Fields.Add(“Department”,[Microsoft.SharePoint.SPFieldType]::MultiChoice,$False,$False,$Choices)

    Thanks

    Reply
    • Here is Syntax:
      public string Add(
      string strDisplayName,
      SPFieldType type,
      bool bRequired,
      bool bCompactName,
      StringCollection choices
      )
      So, use: $list.Fields.Add(“Department”,[Microsoft.SharePoint.SPFieldType]::MultiChoice,$True,$False,$Choices)

      Reply

Leave a Reply

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