1
PS B:\abrabackups> . C:\ps\ConvertTo-JSON.ps1
PS B:\abrabackups> Get-ItemProperty -Path .\AbraSuite01.03.2014 | select Name,CreationTime | ConvertTo-JSON

And that gives me: (formatted for clarity)

{
 "CreationTime": "2014-01-03T16:48:36", 
 "Name": "AbraSuite01.03.2014"
}

Well that's all well and good, but suppose I want my dates in a different format in the JSON string, is there anyway to do this in powershell and still use my shoehorned ConvertTo-JSON.ps1 for powershell 2.0?

1 Answer 1

5

Convert your dates to the format you want before converting to JSON by using a calculated property. For example:

Get-ItemProperty -Path .\AbraSuite01.03.2014 | select Name,@{Name="CreationTime";Expression={$_.CreationTime.ToShortDateString()}} | ConvertTo-JSON

Replace $_.CreationTime.ToShortDateString() with whatever you need to get the date format you're after - but the key here is to use $_.CreationTime to grab the creation time of the object in the pipeline.

Sign up to request clarification or add additional context in comments.

2 Comments

What is the @{...} syntax called?
In this context, it's called a calculated property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.