Pages

Thursday, February 6, 2014

PowerShell Error when inserting dates into a SharePoint list: InvalidCastException

Goal:
Use PowerShell to update a Date column in a SharePoint list.  It is a column that houses Today's date so that workflows and calculations can be run.
Use the Get-Date command to save the date to a variable.

Symptom:
When inserting the variable into a Date/Time type column in a SharePoint list, you get an error:
Specified cast is not valid.
InvalidCastException

Issue:
A date can't be inserted into the SharePoint Date/Time field.  It has to be converted to a string.

Solution:
#get the date and format it and assign it to a variable named date
$date = Get-Date -format d
#convert the date to a string
$date = $date.ToString()

#then insert string variable into list
$item["Today"]=$date


No comments:

Post a Comment