SharePoint Online: PowerShell to Get-Set Date Field Value
PowerShell to Get Date Column Value in SharePoint Online:
SharePoint Online: PowerShell to Set date field
Here is the PowerShell to update date column in SharePoint Online.
How to Convert String to Date?
You can either use: Get-Date "10/02/1984" or [DateTime]"10/02/1984"
#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://crescenttech.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 date column in SharePoint Online.
#Set parameter values $SiteURL="https://crescenttech.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"
No comments:
Please Login and comment to get your questions answered!