Formatting a calculated DateTime Field

Propably an easy question but I’m still pretty new to PS. I’m trying to return only the date part of a datetime field as below but can’t get it working:

get-mailbox -OrganizationalUnit “ou=OUNAME,dc=DCNAME,dc=local” -resultsize unlimited | get-mailboxstatistics | Format-Table DisplayName, @[Name=“LastLogon”;Expression=[$.LastLogonTime.tostring ‘dd/MM/yyyy’}},Itemcount -Autosize | Out-File C:\Temp\Get_permissions\Sharedstats.csv

It complains about an unrecognized token, I think the @.

Anyone able to point me in the right direction?

Hi Barry,

I think you’re better to approach this slightly differently, by doing the formatting of the date object within the Select part. Also, i can see some other problems you may come across, because you are not actually formatting the object in CSV format. CSV will depend on a delimeter of some type, such as a tab or a command. However, Format-Table outputs the data with spaces between the column, which will not be picked up as a native CSV format.

$result = get-mailbox -OrganizationalUnit 'ou=OUNAME,dc=DCNAME,dc=local' -resultsize unlimited |
Get-MailboxStatistics |
Select-Object -Property DisplayName, @{
    n = 'LastLoginTime'
    e = {
        $($_.LastLogonTime).ToString('dd/MM/yyyy')
    }
}, ItemCount

$result |
ConvertTo-Csv -Delimiter ',' -NoTypeInformation |
Out-File -FilePath C:\Temp\Get_permissions\Sharedstats.csv

It’s a good idea if you are referring to a property of a property of an object, or have a complex expression to work out that you simplify it.

One example would be to do this

$($object.property).ToString()

And another.

$prop = $object.property
$propstring = $prop.ToString()

Either way is valid, so it comes down to personal preference.

Tim

Many thanks for your help. I’ve now managed to get this working using the below (in case anyone else is looking for this)

get-mailbox -OrganizationalUnit ‘ou=OUNAME,dc=DOMAINNAME,dc=local’ -resultsize unlimited | Get-MailboxStatistics | Select-Object -Property DisplayName,@{name=‘LastLoginTime’;Expression={$($_.LastLogonTime).ToString(‘dd/MM/yyyy’)}
},ItemCount | ConvertTo-CSV -Delimiter ‘,’ -NoTypeInformation | Out-file -FilePath C:\Temp\SharedStats.csv

cheers Barry.

BTW I noticed i’d not formatted my post properly with details above the last two sets of code. It’s been put in now.