Search code examples
powershelldatetimeloggingevent-log

Add hours to TimeCreated received from Get-WinEvent


I have the below command:

Get-WinEvent -FilterHashtable @{
  Logname      = 'Application'
  ID           = 1000
  ProviderName = 'Application Error'
} -MaxEvents 1 | select TimeCreated

I get the below as output:

TimeCreated
-----------
04-Jan-16 11:29:11 PM

I want this value to be saved into a variable and add two hours into it, like this:

$ErrorTime = Get-WinEvent -FilterHashtable @{
               Logname      = 'Application'
               ID           = 1000
               ProviderName = 'Application Error'
             } -MaxEvents 1 | select TimeCreated

$Time1 = ($ErrorTime).AddHours(2)

But doing the above is not giving me the desired answer. It is not storing the time as value.


Solution

  • You need to expand the TimeCreated property of the event object to get the DateTime value:

    $ErrorTime = Get-WinEvent -FilterHashtable @{
                   Logname      = 'Application'
                   ID           = 1000
                   ProviderName = 'Application Error'
                 } -MaxEvents 1 | Select-Object -Expand TimeCreated
    
    $Time1 = $ErrorTime.AddHours(2)
    

    You can get the date and time portion of the timestamp via the object's Date and Time properties:

    $date = $Time1.Date
    $time = $Time1.Time