Formatting a calculated DateTime Field

Welcome Forums General PowerShell Q&A Formatting a calculated DateTime Field

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
2 years, 10 months ago.

  • Author
    Posts
  • #34288

    Participant
    Points: 0
    Rank: Member

    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?

  • #34290

    Participant
    Points: 60
    Rank: Member

    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.

  • #34292

    Participant
    Points: 0
    Rank: Member

    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

  • #34293

    Participant
    Points: 60
    Rank: Member

    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.

The topic ‘Formatting a calculated DateTime Field’ is closed to new replies.