SharePoint Online: Set Default Value for a List Column using PowerShell

Requirement: SharePoint Online Set Column default value Settings.

How to Set Column default value Settings in SharePoint Online?

When you create a SharePoint list, you specify the type of data stored in each column. However, what if you want to set a default value for a column? Maybe you want to ensure that a particular value is automatically populated in your column whenever a new item is added. This article will show you how to set a default value for a column in the SharePoint Online list.

To preset a default value for a field in the SharePoint Online list,

  1. Go to the List Settings >> Under “Columns”, pick the field by clicking on its title.
  2.  Under the “Default Value” section, you can specify the default value for the field so that it gets populated automatically without user input.
    powershell to set sharepoint online column default value settings

You can also specify the default value for a column at the time of creation!

Configure Default Column Value using PowerShell CSOM

Here is how to set a default value for a column in the SharePoint Online list using PowerShell:

#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"

#Function to set default value of a field
Function Set-SPOFieldDefaultValue($SiteURL, $ListName, $FieldInternalName, $DefaultValue)
{
    Try { 
        #Get Credentials to connect
        $Cred= Get-Credential
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Get the List
        $List=$Ctx.Web.Lists.GetByTitle($ListName)

        #Get the Field
        $Field = $List.Fields.GetByInternalNameOrTitle($FieldInternalName)
        $Field.DefaultValue = $DefaultValue
        $Field.Update()
        $Ctx.ExecuteQuery()
 
        Write-host -f Green "Default Value set for Field Successfully!"
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
#Set parameter values
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Team Documents"
$FieldInternalName = "Classification" #Internal Name
$DefaultValue = "Invoice; Proposal; Quotation"

Set-SPOFieldDefaultValue -SiteURL $SiteURL -ListName $ListName -FieldInternalName $FieldInternalName -DefaultValue $DefaultValue

To set a default value for a SharePoint Online list column, you can use PowerShell to update the field’s “DefaultValue” property. Similarly, you can set the Date column’s default value to Today’s date as:

Set-SPOFieldDefaultValue -SiteURL "https://Crescent.sharepoint.com" -ListName "Tasks" -FieldInternalName "DueDate" -DefaultValue "[Today]"

SharePoint Online: Column Default Value Settings using PnP PowerShell

PnP PowerShell way to set the default value for the SharePoint Online list column:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Team Documents"
$ColumnName = "Classification" #Internal Name
$DefaultValue = "Invoice; Proposal; Quotation"

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

#Set Default Value for the field
Set-PnPField -Identity $ColumnName -List $ListName -Values @{DefaultValue=$DefaultValue}

And the result:

sharepoint online column default value settings

SharePoint Online: Set the default value for a lookup field

To set the default value for a lookup column in a SharePoint Online list, use the following:

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
$FieldName = "ParentProject" #Internal Name

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Set Default Value for the Person column
$DefaultValue = "1;#OS Upgrade" #ID and Text of the Parent Item in the given format

Set-PnPField -Identity $FieldName -List $ListName -Values @{DefaultValue=$DefaultValue}

You can also use the Set-PnPDefaultColumnValues cmdlet to set the default value for a column in the SharePoint Online Document libraries.

Set-PnPDefaultColumnValues -List Documents -Field TaxKeyword -Value "Crescent|Projects|InfraBuild"

SharePoint Online: Set default value for person or group column

Here is how to set the default value for the Person column in a Document Library (This doesn’t work for lists, however!):

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Retail"
$ListName = "Documents"
$FieldName = "ProjectManager" #Internal Name
$DefaultUserID = "i:0#.f|membership|[email protected]"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the User
$User = Get-PnPUser -Identity $DefaultUserID

#Set Default Value for the Person column
Set-PnPDefaultColumnValues -List $ListName -Field $FieldName -Value "$($User.Id);#$($User.LoginName)"

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!

3 thoughts on “SharePoint Online: Set Default Value for a List Column using PowerShell

  • Thanks for this. This shows how to set the default value or a column, but how do you set the column default value settings per folder? I am looking for a way to automate this rather than set for thousands of folders in a document library.

    Reply
  • $FieldInternalName, $DefaultValue are not used in your script, It appears to be missing something in the function Function Set-SPOFieldDefaultValue.

    Reply

Leave a Reply

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