Wednesday, June 10, 2015

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



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...