(view source code of lastboot.vbs as plain text)
Option Explicit' Declare variablesDim blnQuietDim intValidArgsDim colItems, objItem, objWMIServiceDim strBoot, strBootDate, strBootDay, strBootHour, strBootMinsDim strBootMonth, strBootTime, strBootYear, strComputer, strMsg, strQueryintValidArgs = 0
' Check command line parametersWith WScript.Arguments
Select Case .Unnamed.Count
Case 0
' Default if none specified is local computer (".")Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
strQuery = "Select * from Win32_ComputerSystem"
Set colItems = objWMIService.ExecQuery( strQuery, , 48 )
For Each objItem in colItems
strComputer = objItem.Name
NextCase 1
' Command line parameter can either be a computer ' name or "/?" to request online helpstrComputer = UCase( Wscript.Arguments(0) )
Case Else
' Maximum is 1 command line parameter, except for the switchSyntax
End Select
If .Named.Exists( "Q" ) Then
blnQuiet = True
intValidArgs = intValidArgs + 1
End If
If intValidArgs <> .Named.Count Then Syntax
End With
' Enable error handling'On Error Resume Next' Connect to specified computerSet objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
' Display error number and description if applicableIf Err Then ShowError
Set colItems = objWMIService.ExecQuery( "Select * from Win32_OperatingSystem", , 48 )
For Each objItem in colItems
If blnquiet Then
strMsg = Left( objItem.LastBootUpTime, 12 )
ElsestrBootYear = Left( objItem.LastBootUpTime, 4 )
strBootMonth = Mid( objItem.LastBootUpTime, 5, 2 )
strBootDay = Mid( objItem.LastBootUpTime, 7, 2 )
strBootDate = DateValue( strBootDay & "-" & strBootMonth & "-" & strBootYear )
strBootHour = Mid( objItem.LastBootUpTime, 9, 2 )
strBootMins = Mid( objItem.LastBootUpTime, 11, 2 )
strBootTime = strBootHour & ":" & strBootMins
strBoot = strBootDate & ", " & strBootTime
strMsg = "Last boot time of " & strComputer & ": " & strBoot
End If
Next' Display resultsWScript.Echo strMsg
'DoneWScript.Quit(0)
Sub ShowError()
strMsg = vbCrLf & "Error # " & Err.Number & vbCrLf _
& Err.Description & vbCrLf & vbCrLf & vbCrLf
Syntax
End Sub
Sub Syntax( )
strMsg = strMsg _& "LastBoot.vbs, Version 2.00" _
& vbCrLf _& "Display last boot time for any WMI enabled computer" _
& vbCrLf & vbCrLf _
& "Usage: CSCRIPT //NoLogo LASTBOOT.VBS [ computer_name ] [ /Q ]" _
& vbCrLf & vbCrLf _
& "Where: ""computer_name"" is an optional remote computer name" _
& vbCrLf _& " (default is the local computer name)" _
& vbCrLf _& " /Q outputs date/time only, in YYYYMMDDhhmm format" _
& vbCrLf _& " (default is message and DD-MM-YYYY, hh:mm format)" _
& vbCrLf & vbCrLf _
& "Written by Rob van der Woude" & vbCrLf _
& "http://www.robvanderwoude.com"
WScript.Echo strMsg
WScript.Quit 1
End Sub
page last modified: 2025-10-11; loaded in 0.0093 seconds