Powershell automatically converts all the date-time fields to Epoch time when using 'ConvertTo-Json'. Is there a way to prevent this from happening or changing it back to human-readable date-time? For example, check some of the fields in the output
Command:
Get-LocalUser | ConvertTo-Json
Output:
"PasswordChangeableDate": "\/Date(1597311158786)\/",
"PasswordExpires": null,
"UserMayChangePassword": true,
"PasswordRequired": true,
"PasswordLastSet": "\/Date(1597224758786)\/",
"LastLogon": "\/Date(1597238163431)\/",
Is there a way to prevent Powershell from converting the above fields into Epoch time, or converting them back to human-readable date-time?
I suspect that you are using Powershell 5 version. In Powershell 5
ConvertTo-Json
cmdlet is implemented using theJavaScriptSerializer
class. See this table to know the mapping between managed types and JSON equivalent.One solution I could think of is to convert the date to string(Using
ToString()
) before serializing to JSON or to upgrade to Powershell 7 :). This is because PowerShell 7 uses Newtonsoft Json.NET under the hood and the default format used by Json.NET is the ISO 8601 standard:"2012-03-19T07:22Z"
.