SharePoint Online: PowerShell to Get-Set Date Field Value

PowerShell to Get Date Column Value in SharePoint Online:

Date time fields are commonly used in SharePoint Online lists and libraries to capture and display dates and times. These fields can be set to display date only or date and time. By using SharePoint Online PowerShell, you can easily get or set the value of a date time field in a list or library, helping you automate tasks and save time.

Let’s learn how to use PowerShell to get the value of a date and time field in SharePoint Online. This can be useful for reporting or other automation tasks. We’ll also share PowerShell scripts to update date time columns in SharePoint Online.

#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"
 
#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/"
$ListName="Projects"
$FieldName="StartDate"

#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 All List Items
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()     

#Loop through each List Item and get Date Field Value
ForEach($Item in $ListItems)
{ 
    #Get Date & Time of the Field - E.g. Wednesday, February 01, 2017 12:00:00 AM
    $Item[$FieldName]
    
    #Get Date Alone - 2/1/2017
    Get-Date($Item[$FieldName]) -format "d"

    #Get Date in specific format E.g. dd/MM/yyyy HH:mm:ss
    Get-Date ($Item[$FieldName]) -Format "dd-MMM-yyyy"
    
    #Long Date Format - E.g. Wednesday, February 01, 2017
    (Get-Date($Item[$FieldName])).ToLongDateString()

    #Get Time Alone - E.g. 8:00 PM
    (Get-Date($Item[$FieldName])).ToShortTimeString()

    #Get Long Time Format - E.g. 12:30:25 AM
    (Get-Date($Item[$FieldName])).ToLongTimeString()

    #To Universal Date time format: E.g. Tuesday, January 19, 2016 8:00:00 PM
    (Get-Date($Item[$FieldName])).ToUniversalTime()

    #Get Day-Month-Year/Hour-Minute-Second-millisecond from the Date
    (Get-Date($Item[$FieldName])).Year
}

SharePoint Online: PowerShell to Set date field

Here is the PowerShell to update the date time column in SharePoint Online:

#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/"
$ListName="Projects"
$FieldName="StartDate"

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

#Define the CAML Query to filter list items
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View>
    <Query>
        <Where>
            <Eq>
                <FieldRef Name='Title' /><Value Type='Text'>SharePoint 2016 Upgrade</Value>
            </Eq>
        </Where>
    </Query>
</View>"
 
#Get the List Items matching the query
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
  
$ListItems | ForEach-Object { 
    #Set Start Date Field to 7 Days from Today's Date
    $StartDate = (Get-Date).AddDays(7)

    $_[$FieldName] = $StartDate 
    $_.Update()  
    $Ctx.ExecuteQuery()

    Write-host "Date Field Updated!"
}

How to Convert String to Date?
You can either use: Get-Date “10/02/1984″ or [DateTime]”10/02/1984”

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

One thought on “SharePoint Online: PowerShell to Get-Set Date Field Value

  • Thank you for this blog. This is the best SharePoint / PowerShell blog I have ever found. I have been using your reference for a few years, but this is my first time replying.

    Reply

Leave a Reply

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