Solved

How to find out the latest Windows start time

Posted on 2006-11-08
8
2,317 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
  • 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
 

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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

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…
Can you find a fax from a vendor you saved a decade ago in seconds? Have you ever cursed your PC under your breath during an audit because you couldn’t find the requested statement or driver history?  If you answered no to the first question or yes …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now