SharePoint Online: Create a Date and Time Column in the List using PowerShell

How to Add Date and Time Column to SharePoint Online List?

The Date and Time column in SharePoint Online capture date and, optionally time values. To add a Date and Time column to a list, follow these steps:

  1. Browse to your SharePoint Online site and then navigate to the target list in which you want to create a date and time column.
  2. Under the List tab, click on the “Create Column” button in the ribbon.
  3. Provide the Name to your new column, and specify the type as “Date and Time”.
  4. Fill in other optional values and click on “OK” to create a date and time column in the SharePoint Online list.
    SharePoint Online Create Date and Time Column in List using PowerShell

Here is the PowerShell script to create a date and time column for the SharePoint Online list or library.

PowerShell to Add Date and Time Field in SharePoint Online List

Here is how to use PowerShell to add a date and time column to a SharePoint Online list:

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

#Custom function to add column to list
Function Add-DateTimeColumnToList()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $Name,
        [Parameter(Mandatory=$true)] [string] $DisplayName,
        [Parameter(Mandatory=$true)] [string] $Description,
        [Parameter(Mandatory=$true)] [string] $Format,
        [Parameter(Mandatory=$true)] [string] $IsRequired,
        [Parameter(Mandatory=$true)] [string] $FriendlyDisplayFormat,
        [Parameter(Mandatory=$true)] [string] $EnforceUniqueValues
    )

    #Generate new GUID for Field ID
    $FieldID = New-Guid

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup 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()

        #Check if the column exists in list already
        $Fields = $List.Fields
        $Ctx.Load($Fields)
        $Ctx.executeQuery()
        $NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
        if($NewField -ne $NULL)  
        {
            Write-host "Column $Name already exists in the List!" -f Yellow
        }
        else
        {
            #Define XML for Field Schema
            $FieldSchema = "<Field Type='DateTime' ID='{$FieldID}' Name='$Name' StaticName='$Name' DisplayName='$DisplayName' Format='$Format' Required='$IsRequired'  Description='$Description' EnforceUniqueValues='$EnforceUniqueValues' FriendlyDisplayFormat='$FriendlyDisplayFormat' />"
            $NewField = $List.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
            $Ctx.ExecuteQuery()    

            Write-host "New Column Added to the List Successfully!" -ForegroundColor Green  
        }
    }
    Catch {
        write-host -f Red "Error Adding Column to List!" $_.Exception.Message
    }
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$Name="ProjectStartDate" #Column Name
$DisplayName="Project Start Date"
$Description="Enter the Project Start Date"
$Format="DateOnly" #DateTime
$IsRequired = "FALSE"
$EnforceUniqueValues="FALSE"
$FriendlyDisplayFormat="Disabled" #Relative

#Call the function to add column to list
Add-DateTimeColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -IsRequired $IsRequired -Format $Format -EnforceUniqueValues $EnforceUniqueValues -FriendlyDisplayFormat $FriendlyDisplayFormat -Description $Description

PnP PowerShell to Add Date and Time Column to SharePoint Online List

To add the Date and Time column, you can use the Add-PnPField cmdlet. Here’s a PowerShell example:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName= "Projects"

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

#Add Date Time Field to list
Add-PnPField -List $ListName -Type DateTime -DisplayName "Start Date" -InternalName "StartDate" -AddToDefaultView

Similarly, we can create a Date Time column from the XML schema:

#Define XML Schema for Date Field
$FieldXML= "<Field Type='DateTime' Name='StartDate' ID='$([GUID]::NewGuid())' DisplayName='Start Date' Required ='FALSE' Format='DateOnly' FriendlyDisplayFormat='Disabled'></Field>"

#Add DateTime Field to list
Add-PnPFieldFromXml -FieldXml $FieldXML -List $ListName

By following the steps outlined in this article, you can easily create a Date and Time column in SharePoint Online to track the dates and time data. You can add Date and Time column to a SharePoint Online list using both the web browser interface and 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!

2 thoughts on “SharePoint Online: Create a Date and Time Column in the List using PowerShell

  • What about date only formatting with Add-PnPField?

    Reply
  • what to change while doing in my website?

    Reply

Leave a Reply

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