Powershell to group by date time RRS feed

  • Question

  • I wrote below powershell script:

     Get-MessageTrackingLog -Start $start -End $end  -ResultSize unlimited -EventId Send | select $_.Timestamp.ToString("MM/dd/yyyy"), $_.ReturnPath 

    It give me error 

    You cannot call a method on a null-valued expression.
    At line:1 char:1
    +  select $_.Timestamp.ToString("MM/dd/yyyy"), $_.ReturnPath -First 4

    How can I convert the timestamp to MM/dd/yyyy so I can group it by date? thanks


    Tuesday, February 11, 2020 6:38 PM

All replies

  • Without having an Exchange server to check it should be

    Get-MessageTrackingLog -Start $start -End $end  -ResultSize unlimited -EventId Send | 
        Select-Object -Property Timestamp, ReturnPath

    If you want to have the TimeStamp in a particular format you could use a calculated property like this

    Get-MessageTrackingLog -Start $start -End $end  -ResultSize unlimited -EventId Send | 
        Select-Object -Property @{Name='Timestamp';Expression={$_.Timestamp.ToString('MM/dd/yyyy')}}, ReturnPath

    Notice that you provide property names for the Select-Object cmdlet - not pipeline variables. You should always read the complete help including the examples of the cmdlets you're about to use to learn how to use them. ;-)

    Live long and prosper!


    • Edited by BOfH-666 Tuesday, February 11, 2020 10:02 PM
    Tuesday, February 11, 2020 10:00 PM