Link to home
Start Free TrialLog in
Avatar of JohnAtkinson
JohnAtkinsonFlag for United States of America

asked on

How to find out the latest Windows start time

I need a vbs script that I can use on any arbitrary Windows XP system to discover the precise date and time that the system was most recently started.  I'll be placing this script file on a USB key to carry to my clients' computers.  Also, I'd like to know whether I can discover this answer somewhere within the registry or the event log, without a script.  The first provider of the best script will get the points.
Avatar of jkr
jkr
Flag of Germany image

You can find the boot time in the event log by looking for the most recent ID6009 event (http://www.eventid.net/source.asp?source=EventLog) which indicates when the event log service is started on bootup.
Avatar of JohnAtkinson

ASKER

Is it possible for the information to be surfaced by a script?
ASKER CERTIFIED SOLUTION
Avatar of sramesh2k
sramesh2k
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It sure would be great to have this info delivered by a script, showing the actual time of the reboot, rather than the length of time the system has been up.  Maybe it's not possible, though.
Hi John,

To get the actual time (when the system is started), you can use this script, a modified version of the one found at MS scripting site:

Hey, Scripting Guy! How Can I Tell if a Server has Rebooted?:
http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept04/hey0907.mspx

Is modified as:
- - -
strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
    dtmBootup = objOS.LastBootUpTime
    dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
    'dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)
    Wscript.Echo dtmLastBootupTime
    Wscript.Echo dtmSystemUptime
Next
- - -

Sample output
=========

11/12/2006 11:36:48 AM

***** script completed *****
- - -
strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
    dtmBootup = objOS.LastBootUpTime
    dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
    Wscript.Echo dtmLastBootupTime
Next
- - -

Sample output
=========

11/12/2006 11:36:48 AM

***** script completed *****
Thanks for the script, sramesh2k.  Unfortunately, I already accepted the previous answer, thinking that there was no scripting solution.  I don't know how to re-assign the points, or whether that's even possible, or allowed.  

In any case, I took your script, which wouldn't work without the WMIDateStringToDate function, and fleshed it out to report the time of last reboot in both absolute terms and in relative terms.  For the benefit of the community, I'm posting the script here.

------------------------------ cut here --------------------------------------
Function WMIDateStringToDate(dtmBootup)
WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
      Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
      & " " & Mid (dtmBootup, 9, 2) & ":" & _
      Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _
      13, 2))
End Function

strComputer = "."
Dim msg, HoursInPartialDay, MinutesInPartialHour, SecondsInPartialMinute
Dim Days, Hours, Minutes, Seconds
Dim DayMsg, HourMsg, MinuteMsg, SecondMsg
msg = ""
Set objWMIService = GetObject _
      ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
      ("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
      dtmBootup = objOS.LastBootUpTime
      dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
      Days = DateDiff("d", dtmLastBootUpTime, Now)
      Hours = DateDiff("h", dtmLastBootUpTime, Now) Mod 24
      Minutes = DateDiff("n", dtmLastBootUpTime, Now) Mod 60
      Seconds = DateDiff("s", dtmLastBootUpTime, Now) Mod 60
      
      Dim NumberOfComponents
      NumberOfComponents = 0
      
      If Days = 0 Then
            DayMsg = ""
      Else
            NumberOfComponents = NumberOfComponents + 1
            If Days = 1 Then
                  DayMsg = "1 day"
            Else
                  DayMsg = Days & " days"
            End If
      End If
      
      If Hours = 0 Then
            HourMsg = ""
      Else
            NumberOfComponents = NumberOfComponents + 1
            If Hours = 1 Then
                  HourMsg = "1 hour"
            Else
                  HourMsg = Hours & " hours"
            End If
      End If

      If Minutes = 0 Then
            MinuteMsg = ""
      Else
            NumberOfComponents = NumberOfComponents + 1
            If Minutes = 1 Then
                  MinuteMsg = "1 minute"
            Else
                  MinuteMsg = Minutes & " minutes"
            End If
      End If
      
      If Seconds = 0 Then
            SecondMsg = ""
      Else
            NumberOfComponents = NumberOfComponents + 1
            If Seconds = 1 Then
                  SecondMsg = "1 second"
            Else
                  SecondMsg = Seconds & " seconds"
            End If
      End If

      'Format the message in natural language.
      Select Case NumberOfComponents
            Case 0
                  msg = ""
            Case 1
                  msg = "That's " & DayMsg & HourMsg & MinuteMsg & SecondMsg & " ago."
            Case 2
                  msg = ""
                  If Days > 0 Then
                        msg = "That's " & DayMsg & " and "
                  End If
                  If Hours > 0 Then
                        If Len(msg) = 0 Then
                              msg = "That's " & HourMsg & " and "
                        Else
                              msg = HourMsg & " ago."
                        End If
                  End If
                  If Minutes > 0 Then
                        If Len(msg) = 0 Then
                              msg = "That's " & MinuteMsg & " and "
                        Else
                              msg = MinuteMsg & " ago."
                        End If
                  End If
                  If Seconds > 0 Then
                        msg = SecondMsg & " ago."
                  End If
            Case Else                        
                  msg = ""
                  If Seconds > 0 Then
                        msg = " and " & SecondMsg & " ago."
                  End If
                  If Minutes > 0 Then
                        If Len(msg) > 0 Then
                              msg = MinuteMsg & ", " & msg
                        Else
                              msg = " and " & MinuteMsg & " ago."
                        End If
                  End If
                  If Hours > 0 Then
                        If Len(msg) > 0 Then
                              msg = HourMsg & ", " & msg
                        Else
                              msg = " and " & HourMsg & " ago."
                        End If
                  End If
                  If Days > 0 Then
                        If Len(msg) > 0 Then
                              msg = DayMsg & ", " & msg
                        Else
                              msg = " and " & DayMsg & " ago."
                        End If
                  End If
                  msg = "That's " & msg
      End Select

      Wscript.Echo "The date and time of the latest system restart was " &  dtmLastBootupTime & vbCRLF & vbCRLF & msg
Next
------------------------------ cut here --------------------------------------
Found a bug in that previous version of the script.  The updated version is provided below.



'strComputer = "."
Function WMIDateStringToDate(dtmBootup)
WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
      Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
      & " " & Mid (dtmBootup, 9, 2) & ":" & _
      Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _
      13, 2))
End Function

strComputer = "."
Dim msg, HoursInPartialDay, MinutesInPartialHour, SecondsInPartialMinute
Dim Days, Hours, Minutes, Seconds
Dim DayMsg, HourMsg, MinuteMsg, SecondMsg
msg = ""
Set objWMIService = GetObject _
      ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
      ("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
      dtmBootup = objOS.LastBootUpTime
      dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
      Days = DateDiff("d", dtmLastBootUpTime, Now)
      Hours = DateDiff("h", dtmLastBootUpTime, Now) Mod 24
      Minutes = DateDiff("n", dtmLastBootUpTime, Now) Mod 60
      Seconds = DateDiff("s", dtmLastBootUpTime, Now) Mod 60
      Dim NumberOfComponents
      NumberOfComponents = 0
      
      If Days = 0 Then
            DayMsg = ""
      Else
            NumberOfComponents = NumberOfComponents + 1
            If Days = 1 Then
                  DayMsg = "1 day"
            Else
                  DayMsg = Days & " days"
            End If
      End If
      
      If Hours = 0 Then
            HourMsg = ""
      Else
            NumberOfComponents = NumberOfComponents + 1
            If Hours = 1 Then
                  HourMsg = "1 hour"
            Else
                  HourMsg = Hours & " hours"
            End If
      End If

      If Minutes = 0 Then
            MinuteMsg = ""
      Else
            NumberOfComponents = NumberOfComponents + 1
            If Minutes = 1 Then
                  MinuteMsg = "1 minute"
            Else
                  MinuteMsg = Minutes & " minutes"
            End If
      End If
      
      If Seconds = 0 Then
            SecondMsg = ""
      Else
            NumberOfComponents = NumberOfComponents + 1
            If Seconds = 1 Then
                  SecondMsg = "1 second"
            Else
                  SecondMsg = Seconds & " seconds"
            End If
      End If

      'Format the message in natural language.
      Select Case NumberOfComponents
            Case 0
                  msg = ""
            Case 1
                  msg = "That's " & DayMsg & HourMsg & MinuteMsg & SecondMsg & " ago."
            Case 2
                  msg = ""
                  If Days > 0 Then
                        msg = "That's " & DayMsg & " and "
                  End If
                  
                  If Hours > 0 Then
                        If Len(msg) = 0 Then
                              msg = "That's " & HourMsg & " and "
                        Else
                              msg = msg & HourMsg & " ago."
                        End If
                  End If
                  
                  If Minutes > 0 Then
                        If Len(msg) = 0 Then
                              msg = "That's " & MinuteMsg & " and "
                        Else
                              msg = msg &  MinuteMsg & " ago."
                        End If
                  End If
                  
                  If Seconds > 0 Then
                        msg = msg & SecondMsg & " ago."
                  End If
            Case Else                        
                  msg = ""
                  If Seconds > 0 Then
                        msg = " and " & SecondMsg & " ago."
                  End If
                  If Minutes > 0 Then
                        If Len(msg) > 0 Then
                              msg = MinuteMsg & ", " & msg
                        Else
                              msg = " and " & MinuteMsg & " ago."
                        End If
                  End If
                  If Hours > 0 Then
                        If Len(msg) > 0 Then
                              msg = HourMsg & ", " & msg
                        Else
                              msg = " and " & HourMsg & " ago."
                        End If
                  End If
                  If Days > 0 Then
                        If Len(msg) > 0 Then
                              msg = DayMsg & ", " & msg
                        Else
                              msg = " and " & DayMsg & " ago."
                        End If
                  End If
                  msg = "That's " & msg
      End Select

      Wscript.Echo "The date and time of the latest system restart was " &  dtmLastBootupTime & vbCRLF & vbCRLF & msg
Next