kwizcom banner advertisement

How to Add Date Time Field to SharePoint List using PowerShell?

Requirement: Add Date and Time Column to SharePoint list using PowerShell.
add date time column to sharepoint list using powershell

PowerShell Script to add Date time Column to SharePoint list:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Processing Variables
$WebURL="http://portal.crescent.com/sites/Sales"
$ListName="Vendors"
$FieldName="Date of Contract"
$RequiredField=$false

#Get Objects
$Web = Get-SPWeb $WebURL
$List= $Web.Lists[$ListName]

#Add Date and Time Column
$List.Fields.Add($FieldName,"DateTime", $RequiredField)
This Adds a new field.

Set Date Time Column Settings using PowerShell: 
While the above code adds new field to given list, there are additional settings on the column such as: Display Format, Default Value, etc can be set using PowerShell.
powershell to set date time field properties
PowerShell to Update Date Time Column Settings:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Processing Variables
$WebURL="http://portal.crescent.com/sites/Sales"
$ListName="Vendors"
$FieldName="Date of Contract"

#Get Objects
$Web = Get-SPWeb $WebURL
$List= $Web.Lists[$ListName]

#Set Column Settings
$List.Fields[$FieldName].Description = "Enter Date of Join”

#Enforce unique
$List.Fields[$FieldName].EnforceUniqueValues = $false

#Required
$List.Fields[$FieldName].Required = $true

#Date Time Display Format 
$List.Fields[$FieldName].DisplayFormat = "DateOnly"

#Default Value
#$List.Fields[$FieldName].DefaultValue="[today]

#Formula
$List.Fields[$FieldName].DefaultFormula = "=[Today]+7"

#Friendly Display Format
$List.Fields[$FieldName].FriendlyDisplayFormat="Disabled"

$List.Fields[$FieldName].update()

Refer this MSDN link for the complete field properties reference: SPFieldDateTime properties
How to Add Date Time Field to SharePoint List using PowerShell? How to Add Date Time Field to SharePoint List using PowerShell? Reviewed by Salaudeen Rajack on June 10, 2015 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.