I am trying to parse a CSV file in Powershell for a bulk file and metadata upload to SharePoint Online and am failing on the date fields. The incoming date format is '2007/12/24', the expected SPO outgoing date format is '2007-12-24T00:00:00Z'.

I tried the following:

$Metadata.add(  "CreatedDate",   ([datetime]::ParseExact($_."Date Created","yyyy/MM/dd",$null)).ToString("yyyy-MM-ddTHH:mm:ssZ"))

Also Tried this:

$Metadata.add(  "CreatedDate",   (Get-Date ([datetime]::ParseExact($_."Date Created","yyyy/MM/dd",$null)) -Format d).ToString())

And this:

$Metadata.add(  "CreatedDate",   (Get-Date ([datetime]::ParseExact($_."Date Created","yyyy/MM/dd",$null)) -Format u).ToString())

I am getting the following error:

Set-SPOListItem : String was not recognized as a valid DateTime.
At C:\Users\thebear\Desktop\SODA\SODABulkUploadTest3.ps1:108 char:5
Set-SPOListItem -List SEDA -Identity $($Upload.ListItemAllFields. ...

Any idea what I might be doing wrong?


Turns out there are 4 date fields in the CSV file, and if any of them are empty, all of the examples above fail! I added if ([bool]$_."Date Created") {...} to each of the date lines to resolve this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.