How to Add a Choice Column to a SharePoint Online List Using PnP PowerShell?

Recently, one of my team members was required to add a choice column to a SharePoint Online list. I suggested PnP PowerShell cmdlets. In this tutorial, I will show you how to add a choice column to a SharePoint Online list using PnP PowerShell.

Here, I have created a SharePoint Online list that has the Title column, and you can see the list in the screenshot below:

Add a Choice Column to a SharePoint Online List

Here, I will add a choice column as a “Department” with several options in it.

Add a Choice Column to a SharePoint Online List Using PnP PowerShell

We will add the “Department” choice column to the “Employee” list. The choices will be “HR”, “Finance”, “IT”, and “Marketing”.

Here is the complete PnP PowerShell script:

# Connect to SharePoint Online
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerShellFAQsNew/" -Interactive

# Ensure the Employees list exists
$list = Get-PnPList -Identity "Employees"
if ($null -eq $list) {
    Write-Host "The list 'Employee' does not exist." -ForegroundColor Red
    exit
}

# Define the XML for the choice field
$fieldXml = @"
<Field Type='Choice' DisplayName='Department' Name="Department" Format='Dropdown' FillInChoice='FALSE'>
    <Default>HR</Default>
    <CHOICES>
        <CHOICE>HR</CHOICE>
        <CHOICE>Finance</CHOICE>
        <CHOICE>IT</CHOICE>
        <CHOICE>Marketing</CHOICE>
    </CHOICES>
</Field>
"@

# Add the choice field to the Employee list using XML
try {
    Add-PnPFieldFromXml -List "Employees" -FieldXml $fieldXml
    Write-Host "Choice field 'Department' added successfully." -ForegroundColor Green
} catch {
    Write-Host "Error adding choice field: $_" -ForegroundColor Red
    exit
}

I executed the above PnP PowerShell script, and you can see the output in the screenshot below:

Add a Choice Column to a SharePoint Online List Using PnP PowerShell

The Add-PnPFieldFromXml cmdlet adds the choice field to the “Employee” list. The try block catches any errors that occur during this process and prints an error message.

Set Default Value for the Choice Column

If you want to set a default value for the choice column in the SharePoint Online list, you can write the PnP PowerShell script below.

# Set the default value of the Department choice column
try {
    Set-PnPField -List "Employee" -Identity "Department" -Values @{DefaultValue="HR"}
    Write-Host "Default value for 'Department' set to 'HR'." -ForegroundColor Green
} catch {
    Write-Host "Error setting default value: $_" -ForegroundColor Red
}

Check out How to Check if a List Exists in SharePoint Site using PnP PowerShell?

Add Choice Column to a SharePoint Online List using Add-PnPField

Let me show you another method to add a choice column to a SharePoint Online list using Add-PnPField.

Here is the complete script from connecting to SharePoint Online to adding the “Job Role” choice column and setting its default value:

# Connect to SharePoint Online
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerShellFAQsNew/" -Interactive

# Add a choice column to the Employees list
Add-PnPField -List "Employees" -DisplayName "Job Role" -InternalName "JobRole" -Type Choice -Group "Custom Columns" -AddToDefaultView `
-Choices "Manager", "Developer", "Analyst", "Support"

# Set the default value for the Job Role choice column
Set-PnPField -List "Employees" -Identity "JobRole" -Values @{DefaultValue="Developer"}

# Verify the column addition
$field = Get-PnPField -List "Employees" | Where-Object {$_.InternalName -eq "JobRole"}
if ($field -ne $null) {
    Write-Host "Choice column 'Job Role' added successfully." -ForegroundColor Green
} else {
    Write-Host "Failed to add choice column 'Job Role'." -ForegroundColor Red
}

Here is the explanation:

  • -List "Employees" specifies the list where the column will be added.
  • -DisplayName "Job Role" sets the display name of the column.
  • -InternalName "JobRole" sets the internal name of the column.
  • -Type Choice specifies the type of the column.
  • -Group "Custom Columns" groups the column under “Custom Columns”.
  • -AddToDefaultView adds the column to the default view of the list.
  • -Choices "Manager", "Developer", "Analyst", "Support" specifies the choices for the column.

Once you execute the script, then you can see the output in the screenshot below:

How to Add a Choice Column to a SharePoint Online List Using PnP PowerShell

If you open the SharePoint Online list, you can see the column that got added and the choices. See the screenshot below:

sharepoint list add choice column pnp powershell

I have explained how to add a choice column to a SharePoint Online list using two methods in this tutorial. I hope the above two examples will help you! Do let me know in the comment below if you have any issues.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.