Solved

How to find out the latest Windows start time

Posted on 2006-11-08
8
2,322 Views
Last Modified: 2011-10-03
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.
0
Comment
Question by:JohnAtkinson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 17903822
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.
0
 

Author Comment

by:JohnAtkinson
ID: 17903911
Is it possible for the information to be surfaced by a script?
0
 
LVL 34

Accepted Solution

by:
sramesh2k earned 500 total points
ID: 17904181
Hi John,

You may use the Uptime.exe utility from Microsoft.

Determine the current system uptime:
http://www.winxptutor.com/uptime.htm

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:JohnAtkinson
ID: 17921949
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.
0
 
LVL 34

Expert Comment

by:sramesh2k
ID: 17924311
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 *****
0
 
LVL 34

Expert Comment

by:sramesh2k
ID: 17924312
- - -
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 *****
0
 

Author Comment

by:JohnAtkinson
ID: 17932367
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 --------------------------------------
0
 

Author Comment

by:JohnAtkinson
ID: 17934277
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
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you build your web application in Visual Studio you'll get at least a few binaries, or .DLL, files in your bin folder. However, there is more compiling to be done. Normally this would happen when an ASP.NET resource within the web site is request…
If you have done a reformat of your hard drive and proceeded to do a successful Windows XP installation, you may notice that a choice between two operating systems when you start up the machine. Here is how to get rid of this: Click Start Clic…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question